microsoft / microsoft/TypeScript
Removing optional modifier also removes `undefined` from value type
Open
Nobody has claimed this yet.
Bug
Domain: Mapped Types
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.3.3333
Search Terms: NonPartial, remove optional modifier
Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
interface OptClass {
opt?: number;
}
type NonPartialIsh = {[K in keyof OptClass]-?: OptClass[K] | undefined};
const test = {opt: undefined};
verify<NonPartialIsh>(test); // should NOT be error, but shows error
function verify<T>(a: T) {}
Expected behavior:
- Should not have any error.
NonPartialish.opttype should supportundefined.
Actual behavior:
- Throws the following error:
ERROR(12,23): : Argument of type '{ opt: undefined; }' is not assignable to parameter of type 'NonPartialIsh'.
Types of property 'opt' are incompatible.
Type 'undefined' is not assignable to type 'number'.
Transpiled code follows despite errors.
NonPartialish.opttype does not supportundefined.
Related Issues:
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 self-contained example with TypeScript 3.3.3333 and compare the mapped type's behavior when removing the optional modifier. Trace how the compiler handles the property type in this mapped-type case; done means the example accepts {opt: undefined} and the resulting property type supports undefined.
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
- 38/100