-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Using strictNullChecks with/without noImplicitAny changes type inferrence for let variables with null
or undefined
initial values
#55265
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
Comments
null
and undefined
initial valuesnull
or undefined
initial values
This is working as intended. See #17651 (comment) and other issues (search for |
Ah ok I see, I'll close this then |
@eliasm307 To be clear, it's not actually inferring The rationale is that if you're initializing something with |
@fatcerberus thanks for the clarification and the details, I appreciate it. I did understand something similar to that from MartinJohns comment however it's interesting learning about how TS is working under the hood. Thanks to you both for the information! |
@fatcerberus Also, for interest, I see your name alot in discussions on this project (issues etc) and you seem very knowledgeable but I see you don't have a maintainer tag, are you a former maintainer or an enthusiast? |
I'm not a maintainer, no. But you can learn a lot about how TypeScript works internally by hanging around here for a while and seeing how the maintainers respond to issues--things get blocked by design limitations a lot so the devs tend to be pretty transparent about what goes on under the hood π |
@fatcerberus thats cool, you must have been "hanging around" here for quite a while then π |
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
Also might be related to #45592
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?strict=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&esModuleInterop=false&declaration=false&noErrorTruncation=true&noImplicitOverride=false&ts=5.2.0-beta#code/DYUwLgBAHhC8EDsCuxgG4CwAoA9DiEAegPzbaiQCecESCAJiAGYCWCI92eBJQA
π» Code
For the following code:
With just
strictNullChecks
enabled in tsconfig.json the values are inferred asnull
andundefined
(see playground)However with
strictNullChecks
andnoImplicitAny
the types are now both inferred asany
(see playground)π Actual behavior
With
strictNullChecks
andnoImplicitAny
let variables withnull
orundefined
initial values are inferred asany
typeπ Expected behavior
With
strictNullChecks
andnoImplicitAny
let variables withnull
orundefined
initial values should be inferred asnull
andundefined
respectfully to match the normalstrictNullChecks
behaviour.noImplicitAny
is meant to improve type strictness however with this behaviour it actually means some types become less strict and could allow runtime issues that would have been caught without the option.I am migrating some repositories to use
noImplicitAny
incrementally, however I noticed when a package (lets call it ParentPackage) that hadnoImplicitAny
off was using a dependency package (lets call it ChildPackage) that hadnoImplicitAny
on then I was getting type errors in node_modules.This was due to cases like in the example code where a variable has an initial
null
value that gets re-assigned, however since the behaviour in ParentPackage withoutnoImplicitAny
is to infer the type asnull
there was anis not assignable to type 'null'
type error, where the ChildPackage repo didnt have this error withnoImplicitAny
The text was updated successfully, but these errors were encountered: