microsoft / microsoft/TypeScript
Generic type not assignable if extends type with optional field and passed object does not include the optional field
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.9.2
Search Terms:
optional generic "not assignable"
Code
const foo = <V extends { s?: string }>(v: V) => {};
// This works:
foo({ s: "1", v: 2 });
// This doesn't work:
foo({ v: 2 }); // Argument of type '{ v: number; }' is not assignable to parameter of type '{ s?: string | undefined; }'. Object literal may only specify known properties, and 'v' does not exist in type '{ s?: string | undefined; }'.
Compare with
const bar = <V extends { }>(v: V) => {};
// Both work:
bar({ s: "1", v: 2 });
bar({ v: 2 });
Expected behavior:
Both statements in the first snippet should compile.
Actual behavior:
foo({ v: 2 }); doesn't compile for const foo = <V extends { s?: string }>(v: V) => {};
Related Issues:
Possible remotely related... https://github.com/microsoft/TypeScript/issues/13747
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 by running the TypeScript 3.9.2 reproduction in the provided Playground link and compare the two generic calls. Trace the type-checking behavior for excess properties on a generic constraint with an optional field. Done means both calls in the first snippet compile without changing the behavior shown for the second snippet.
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
- 35/100