microsoft / microsoft/TypeScript
Assertion call in optional chain should not narrow variable
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 4.1.3
Search Terms: assert optional call
Expected behavior:
No narrowing of v.
Maybe an error indicating that assertion calls don't work with optional chaining.
Actual behavior:
Assertion call narrows v. If strictNullChecks is enabled, it works as expected.
Related Issues:
Code
// @strictNullChecks: false
declare const obj: {
assert?(v: any): asserts v;
}
declare const v: boolean;
obj.assert?.(v);
v; // should not be narrowed to 'true'
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2017",
"jsx": "React",
"module": "ESNext"
}
}
Playground Link: Provided
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Playground Link and reproduce the optional assertion-call example with strictNullChecks disabled, comparing the observed narrowing with the expected behavior. Trace the compiler's control-flow narrowing for optional calls; done means v is not narrowed after obj.assert?.(v), or the unsupported combination is diagnosed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100