We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: Nightly
Search Terms: "argument not assignable" "optional property" 2345
Expected behavior: Typescript should realise that the property has been checked for truthiness and cannot be undefined, no error should be raised
Actual behavior: Typescript claims that the property could still be undefined and raises this error:
Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.(2345)
Related Issues: #37240 #35964 #36193
Code
interface IFoo { bar?: string; } type Obj = {[key: string]: IFoo} function print(str: string) { return console.log(str); } export function check(obj: Obj, key: string) { if (obj[key]?.bar) { print(obj[key]?.bar); } }
function print(str) { return console.log(str); } export function check(obj, key) { var _a, _b; if ((_a = obj[key]) === null || _a === void 0 ? void 0 : _a.bar) { print((_b = obj[key]) === null || _b === void 0 ? void 0 : _b.bar); } }
{ "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
The text was updated successfully, but these errors were encountered:
Duplicate #10530
Sorry, something went wrong.
No branches or pull requests
TypeScript Version: Nightly
Search Terms: "argument not assignable" "optional property" 2345
Expected behavior: Typescript should realise that the property has been checked for truthiness and cannot be undefined, no error should be raised
Actual behavior: Typescript claims that the property could still be undefined and raises this error:
Related Issues:
#37240
#35964
#36193
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: