-
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
Interface Index Signatures + Strict Null Checking #9235
Comments
Yes, you could argue that every index signature should have |
@ahejlsberg I didn't think you could add I do see the argument about the array case, but I feel like you'd catch a lot more user issues with this check. Eventually, flow control could even detect that However, isn't this a similar case as And because I do feel it's going to be painful, anyone that has written code with an index signature today will break on 2.0 immediately with Edit: Also, |
This issue has an "interesting" consequence - a compilation with So far this is the only way that I know of that happening, i.e. the validity of the .d.ts being dependent on the strictNullChecks flag. It would be nice to have the property that independent of the compilation flags, produced |
If the concern is just breaking folks who have Reposting the rest of my response from #11122 since that was closed as dupe and I am bummed about the current decision: +1 for fixing this. I've been bitten by this compromise a couple times now so count me in for more safety not less!
If folks are using an unsafe pattern everywhere, maybe that's exactly what should happen. I don't find it particularly onerous. I guess I'd like to see some data on exactly how much impact this would have on existing TypeScript programs before we write it off as making the language unusable.
Right now the sentiment I've seen from TS team is that 1 is negligible, 2 is huge, and 3 is prohibitive. Are there numbers to back up these hunches? Some alternatives to
The same could be argued for |
If authors enable I'm not too familiar with many other type systems, but Scala does have Indeed there would be slightly more boilerplate if we did return Without this there will be a whole class of bugs that TypeScript will never catch. 😒 😢 |
Maybe there is the need to distinguish somehow between properties which are optional and properties which are present but may have undefined as value? |
@Flarna It actually does. Both for functions and objects. type Bag = {
optional?: number;
mustBePresent: number|undefined;
}
const bag: Bag = {
mustBePresent: 3 // type error if missing
// we dont need to specify `optional`
};
function f(mustBePresent: number|undefined, optional?: number) {}
f(undefined); // OK
f(15, undefined); // OK
f() // not OK, `mustBePresent` is missing |
Sorry, seems I was not clear with my comment. If I have a type like that
I would expect that I can iterate over all properties and get only numbers. But actually I may get also undefined. If an index signature is used this is different:
Here the properties are optional but if they are present they are numbers. |
Maybe it could be handled with extra compiler option? |
This really seems like a half-hearted solution. I expected the |
😭 |
@RyanCavanaugh Sadly I'm already starting to see this leak into projects that now are forced to enable |
The current behavior is just wrong, and I don't understand how this can be labeled as "working as intended". It is a fact that when accessing an index signature, it can always be If a user does not want this, nobody forces him to enable strict null checks. It's opt-in. But the current situation causes huge problems with declaration files like in moment, where we do not have the possibility to fix this without a breaking change. To make them compatible with TS2 & strict null checks we would have to add |
For those interested I created a proposal that this behaviour (index signatures returning |
How about a We can create abstractions like |
Hasn't Douglas Crockford spent the last few years telling everybody how he stopped using This is just a degenerate case of a bigger problem:
We "know" that in each case, the accessor should return a value, exactly the same way that we "know" that an array index will return a value when we're looping from 0 to
That's why index signatures should in fact always have an implied |
I'm in the same boat. Furthermore, sometimes you have an object like: |
I've noticed that about narrowed types... It looks like typescript doesn't have |
@ccorcos it has, you just need to set the right |
Ah thanks |
…2828) Two changes: 1) We are now allowing the parameters of a request to be typed. This adds more type safety to consumers. 2) We support optional parameters in the request. For (2), the root cause is this issue in how Typescript handles --strictNullChecking: microsoft/TypeScript#9235
TypeScript Version:
Version 1.9.0-dev.20160617-1.0
Code
Expected behavior: No error. Wouldn't the fact that it's an index signature implicitly signal that it could also be
undefined
?Actual behavior:
Edit: On the other side of the coin with this usage:
Should
bar
be type ofstring | undefined
?The text was updated successfully, but these errors were encountered: