You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is this a regression in control flow-based type analysis for --strictNullChecks? #25109
constobj: {[key: string]: string|undefined}={}constwithAssignment=(key: string): string|null=>{constvalue=obj[key];returntypeofvalue==='undefined'
? null
: value;}constwithTernary=(key: string): string|null=>returntypeofobj[key]==='undefined'
? null
: obj[key];// Type 'string | null | undefined' is not assignable to type 'string | null'.// Type 'undefined' is not assignable to type 'string | null'.constwithIf=(key: string): string|null=>{if(typeofobj[key]==='undefined'){returnnull;}else{returnobj[key];// Type 'string | undefined' is not assignable to type 'string | null'.// Type 'undefined' is not assignable to type 'string | null'.}}
Expected behavior:
Just like the example in the docs but with an undefined instead of a string.
No error.
No additional explicit check.
The property does not change in this simple statement so the type assertion should be OK.
Can we add this to the docs and how do we get that going? This should be as accessible and well documented as the CFA docs for typeof x === 'string. No one else should have to waste their time with this.
Since it should almost always be possible to write const j = list[i] instead, this shouldn't be too burdensome.
That wouldn't pass code review in the large functional-inspired React/Redux repo I with. Is there any way to opt-in to this check? How do we get that going?
how is it different than the docs on control flow based type analysis? Does that performance hit come from checking if the property access/getter modifies the object?
The difference is x.y vs x[expr].
Notice that CFA works perfectly fine when the keys are static, e.g. if (a.b.c.d) { a.b.c.d.toString() } is OK. The issue is when the key is computed (obj[key]) because now we go from a static lookup of variables + properties we already know about to an infinite matrix of obj vs key, plus the associated cost of determining whether or not key has been mutated between accesses.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
RyanCavanaugh commentedon Jun 20, 2018
See #11483 / #10530
seanpoulter commentedon Jun 20, 2018
Thanks for the speedy reply Ryan Cavanaugh (@RyanCavanaugh). Since this is the same as #11483, how is it different than the docs on control flow based type analysis? Does that performance hit come from checking if the property access/getter modifies the object?
I'd like to propose two actions:
Can we add this to the docs and how do we get that going? This should be as accessible and well documented as the CFA docs for
typeof x === 'string. No one else should have to waste their time with this.In #11483 you said:
That wouldn't pass code review in the large functional-inspired React/Redux repo I with. Is there any way to opt-in to this check? How do we get that going?
RyanCavanaugh commentedon Jun 20, 2018
The difference is
x.yvsx[expr].Notice that CFA works perfectly fine when the keys are static, e.g.
if (a.b.c.d) { a.b.c.d.toString() }is OK. The issue is when the key is computed (obj[key]) because now we go from a static lookup of variables + properties we already know about to an infinite matrix ofobjvskey, plus the associated cost of determining whether or notkeyhas been mutated between accesses.We'd take a doc PR to improve that (maybe a section about arrays / indexed access?) - https://github.com/Microsoft/TypeScript-Handbook
obj[key]!will removenull/undefinedfrom the typetypescript-bot commentedon Jul 5, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.