-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict null checks, index signatures, and optional properties. #17277
Comments
I'm sure this is intended behavior. In the presence of declare let thing: Thing;
thing.foo.toString(); // no possibly-undefined warning As you noted, there's not much difference in TypeScript between an optional property and a required property with a possibly- If you are trying to say that interface Thing {
[key: string]: string | number | boolean | undefined;
} If your main issue is with the way TypeScript treats missing keys, then maybe you should head over to #13195 and upvote and/or contribute. Cheers! |
I guess my main argument is the inconsistency. If we're going to say "okay, index signatures may be |
Index signature is a constraint on the type. it is assumed that all properties would fall under this constraint.. otherwise |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
This may be related to #13778
I want to define a type which restricts the allowable values of properties to a specific set of types. Sub-types may define certain properties to be a more specific subset of the allowable types (think JSON object but with well-formed, known keys).
The code below is a simplified demonstration, but I run into problems with sub-types specifying optional properties when
strictNullChecks
is turned on. Compilation fails with a message saying the property is incompatible with the index signature. It strikes me as odd that this is not allowed, at the same time the index signature does not have an implicitundefined
. If anything, the well typed property is more safe than using the index signature. My intuition is not than an index signature should have an implicitundefined
, but that the below code should be allowed withstrictNullChecks
. I realize thatfoo?: T
is treated the same asfoo: T | undefined
by the compiler, but there is a distinct difference between the lack of presence of key, and a key withundefined
as a value. Usually the distinction doesn't matter, but in this case it does.TypeScript Version: 2.4.0
Code
Expected behavior:
Compiles with
strictNullChecks
Actual behavior:
Does not compile with
strictNullChecks
The text was updated successfully, but these errors were encountered: