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: 3.7.2
Search Terms: Optional chaining
Expected behavior: Everything compiles successfully.
Actual behavior: For the x variable:
x
Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'.(2345)
Related Issues:
Code
function checkExists(x: null | undefined, msg?: string, ...args: any[]): never; function checkExists<T>(x: T | null | undefined): T; function checkExists<T>(x: T | null | undefined): T { if (x == null) { throw new Error('argument is null'); } return x; } type SomeType = { prop: number | undefined; } function foo(): SomeType | undefined { return { prop: 123 }; } function bar(val: number) {} // Doesn't work const val = foo(); const x = checkExists(val?.prop); bar(x); // Works const y = checkExists(val && val.prop); bar(y); // Works const z = checkExists(val?.prop as number | undefined); bar(z);
"use strict"; var _a, _b; function checkExists(x) { if (x == null) { throw new Error('argument is null'); } return x; } function foo() { return { prop: 123 }; } function bar(val) { } const val = foo(); const x = checkExists((_a = val) === null || _a === void 0 ? void 0 : _a.prop); bar(x); const y = checkExists(val && val.prop); bar(y); const z = checkExists((_b = val) === null || _b === void 0 ? void 0 : _b.prop); bar(z);
{ "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:
Please try with typescript@next as this issue is already fixed in master
typescript@next
Sorry, something went wrong.
Thanks. Will keep an eye on stable releases.
No branches or pull requests
TypeScript Version: 3.7.2
Search Terms:
Optional chaining
Expected behavior:
Everything compiles successfully.
Actual behavior:
For the
x
variable:Related Issues:
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: