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
// A *self-contained* demonstration of the problem follows...// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.typeEventType='click'|'dblclick'consthandlerMap: {[PinEventType]?: any[]}={}functionaddHandler<PextendsEventType>(evType: P){consthandlerList=handlerMap[evType]||[]handlerList.push({})// Error here: Object is possibly 'undefined'handlerMap[evType]=handlerList}
#29317 should be the fix here (which I have mentioned in Collins Abitekaniza (@collin5) 's PR). Specifically, the type of a || b should be calculated as (typeof a & not FalseyTypes) | typeof b, where FalseyTypes is 0 | false | "" | undefined | null.
Stanislav Svirid (@ktoto) your issue is different - since State has an index signature, we don't track refinements on individual property lookups within it - see #17960 and #29042 (and go updoot those issues so they don't sit in the backlog).
Reacted by David Enke, benvinova, peng-song-aceik, akshay upadhya, mrtnmck, Luis Custodio, Peter Hozák, Denkan, Pachito Marco Calabrese, Vladyslav and 17 more
Reacted by <:::::[]=¤༺Omͥega๖༻(▀̿̿Ĺ̯̿̿▀̿ ̿), Matheus Bueno, davidxdiaz and Leonardo Larocca
typeKey="key1"|"key2";typeKeys={[PinKey]?: {keyProp: string};};constkeys: Keys={key1: {keyProp: "keyPropVal"},};letsomeKey!: Key;if(keys[someKey]){// This shows Object is possibly 'undefined'keys[someKey].keyProp;}
Output
"use strict";constkeys={key1: {keyProp: "keyPropVal"},};letsomeKey;if(keys[someKey]){// This shows Object is possibly 'undefined'keys[someKey].keyProp;}
This warning is shown by the editor when we provide the body definition of some variable in the form of the interface and add some of the fields as optional
Eg:-
interface Blog {
autherName: string;
contents?: any[],
id: string
}
While using this interface Blog in our code if we are trying to do some loop on Blog.contents
Eg:-
const newBlog: Blog = JSON.parse(blog);
for (const content of newBlog.contents) {`
console.log("content ", content );
}
This will error in for loop declaration for (const content of newBlog.contents) because we have said in an interface that contents property in Blob object can be or cannot be there hence we first need to check in code if the property is present then we should use it. As follows
Eg:-
if (newBlog.contents && newBlog.contents.length > 0) {
for (const content of newBlog.contents) {
console.log("content ", content );
}
}
This warning is shown by the editor when we provide the body definition of some variable in the form of the interface and add some of the fields as optional
Eg:-
interface Blog {
autherName: string;
contents?: any[],
id: string
}
While using this interface Blog in our code if we are trying to do some loop on Blog.contents
Eg:-
const newBlog: Blog = JSON.parse(blog);
for (const content of newBlog.contents) {`
console.log("content ", content );
}
This will error in for loop declaration for (const content of newBlog.contents) because we have said in an interface that contents property in Blob object can be or cannot be there hence we first need to check in code if the property is present then we should use it. As follows
Eg:-
if (newBlog.contents && newBlog.contents.length > 0) {
for (const content of newBlog.contents) {
console.log("content ", content );
}
}
Locking because everyone is posting about all of their "is possibly undefined" code snippets here without context for the original post.
The inability to handle x || [] when x is a type parameter is a current design limitation wherein we need to have some new mechanism for describing higher-order facts about generic type parameters; today if something is T and we know it's truthy or falsy, there's no mechanism for us to better describe its assignability to any given target type. Everything we've tried so far here has been a performance or usability disaster so it's on the back burner for the time being until we can hopefully have some better insight on how to represent this in a more ergonomic/efficient way.
Activity
collin5 commentedon Feb 1, 2019
Will be giving this a try.
ktoto commentedon Feb 18, 2019
It's the same issue?
Playground link : https://www.typescriptlang.org/play/#src=interface%20State%20%7B%0D%0A%20%20%20%20%5Bi%3A%20string%5D%3A%20%7Bdata%3F%3A%20%7Bid%3Astring%7D%7D%20%7C%20undefined%0D%0A%20%7D%0D%0A%0D%0Adeclare%20var%20x%3A%20State%3B%0D%0A%0D%0Aconst%20id%20%3D%20'xx'%3B%0D%0A%0D%0Aconst%20y%20%3D%20x%5Bid%5D%20%26%26%20x%5Bid%5D.data%3B%0D%0A%0D%0Aconst%20yId%20%3D%20y%20%26%26%20y.id%3B%0D%0A
weswigham commentedon Mar 14, 2019
#29317 should be the fix here (which I have mentioned in Collins Abitekaniza (@collin5) 's PR). Specifically, the type of
a || bshould be calculated as(typeof a & not FalseyTypes) | typeof b, whereFalseyTypesis0 | false | "" | undefined | null.Ryan Cavanaugh (@RyanCavanaugh) given that, should we move this out of
3.4, since we won't be shipping them in 3.4?weswigham commentedon Mar 14, 2019
Stanislav Svirid (@ktoto) your issue is different - since
Statehas an index signature, we don't track refinements on individual property lookups within it - see #17960 and #29042 (and go updoot those issues so they don't sit in the backlog).MufidJamaluddin commentedon May 10, 2019
I have same issue. :(
Typescript Version : 3.4.5
troy351 commentedon May 10, 2019
Mufid Jamaluddin (@MufidJamaluddin) duplicate of #7719
Workaround is use
kelas!.iskelasmicky2be commentedon Jun 13, 2019
Same issue as Mufid Jamaluddin (@MufidJamaluddin)
I have a checker in place to avoid duplicating code but Typescript ignores it and output this error.
27 remaining items
RyanCavanaugh commentedon Jan 23, 2020
Tracking at #7719
typescript-bot commentedon Jan 26, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
andreialecu commentedon May 15, 2020
Ryan Cavanaugh (@RyanCavanaugh) did you link the wrong issue? That one has been closed since 2018.
ecklf commentedon May 15, 2020
Also having this issue
Code
Output
Compiler Options
{ "compilerOptions": { "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "useDefineForClassFields": false, "alwaysStrict": true, "allowUnreachableCode": false, "allowUnusedLabels": false, "downlevelIteration": false, "noEmitHelpers": false, "noLib": false, "noStrictGenericChecks": false, "noUnusedLocals": false, "noUnusedParameters": false, "esModuleInterop": true, "preserveConstEnums": false, "removeComments": false, "skipLibCheck": false, "checkJs": false, "allowJs": false, "declaration": true, "experimentalDecorators": false, "emitDecoratorMetadata": false, "target": "ES2017", "module": "ESNext" } }Playground Link: Provided
Casa-Cloud commentedon May 16, 2020
Hello All,
This warning is shown by the editor when we provide the body definition of some variable in the form of the interface and add some of the fields as optional
Eg:-
While using this interface Blog in our code if we are trying to do some loop on Blog.contents
Eg:-
This will error in for loop declaration for (const content of newBlog.contents) because we have said in an interface that contents property in Blob object can be or cannot be there hence we first need to check in code if the property is present then we should use it. As follows
Eg:-
This is one way to resolve this warning.
Happy Coding!
harishkshetty commentedon May 17, 2020
Thanks alokadhao20,It solved issue for me.
subins2000 commentedon Jul 4, 2020
Ryan Cavanaugh (@RyanCavanaugh) did you link the wrong issue ? That one has been closed since 2018.
RyanCavanaugh commentedon Jul 8, 2020
Locking because everyone is posting about all of their "is possibly undefined" code snippets here without context for the original post.
The inability to handle
x || []whenxis a type parameter is a current design limitation wherein we need to have some new mechanism for describing higher-order facts about generic type parameters; today if something isTand we know it's truthy or falsy, there's no mechanism for us to better describe its assignability to any given target type. Everything we've tried so far here has been a performance or usability disaster so it's on the back burner for the time being until we can hopefully have some better insight on how to represent this in a more ergonomic/efficient way.