microsoft / microsoft/TypeScript
Readonly properties type narrowing doesn't flow into inner function scopes
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.3.0-dev.20190105
Search Terms: Readonly, strictNullChecks, optional, inner function
Code
Using strictNullChecks: true:
interface Thing {
foo?: (a: number) => boolean;
}
const t: Readonly<Thing> = {
foo: (a) => true,
};
if (t.foo !== undefined) {
const bar = () => {
t.foo(5);
}
}
Expected behavior:
👍 Compile
Actual behavior:
On the expression t.foo(5); the following error:
Cannot invoke an object which is possibly 'undefined'.
(property) foo?: ((a: number) => boolean) | undefined
Related Issues:
- Possibly a duplicate https://github.com/Microsoft/TypeScript/issues/22137, but I wasn't sure.
- Following the theory of this issue, this should be seen as a bug: https://github.com/Microsoft/TypeScript/issues/10927.
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 supplied TypeScript reproduction and its Playground link, focusing on control-flow narrowing for the readonly optional property inside the arrow function. Confirm the reported error and compare the behavior with the related issues; done means the example compiles without incorrectly allowing an undefined call.
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
- Clearly specified
- Newbie friendliness
- 42/100