Removing optional modifier also removes `undefined` from value type
See original GitHub issueTypeScript 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.opt
type 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.opt
type does not supportundefined
.
Related Issues:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (3 by maintainers)
Top Results From Across the Web
remove null or undefined from properties of a type
I need to define a generic type called NoUndefinedField<T> such that NoUndefinedField<Type1> gives the same type as Type3 and the same type as ......
Read more >Remove Null and Undefined from a Type in TypeScript
Use the `NonNullable` utility type to remove `null` and `undefined` from a type in TypeScript. The `NonNullable` utility type constructs a new type...
Read more >How to Convert Object Props With Undefined Type to Optional ...
First I thought about mapped types as we can add or remove optional modifiers in mapped types. Sadly, this won't work since we...
Read more >Mapped Type Modifiers in TypeScript - Marius Schulz
With TypeScript 2.8, mapped types have gained the ability to add or remove a particular modifier from a property.
Read more >typescript-cheatsheet - GitHub Pages
The cheatsheet contains references to types, classes, decorators, ... Arrow functions may also be defined with default argument values in case no respective ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Unfortunately missing and undefined aren’t consistently two different things in TypeScript; see #13195.
Relevant documentation on the behavior of
-?
:But in
should
undefined
be excluded from the original type of the property (T[K]
) or the mapped type of the property (Foo<T[K]>
)? I’d kind of expect it to be the former but it looks like it’s actually the latter.Assuming we can’t destabilize the current behavior, someone who wants to strip the optional modifier off property keys but hold on to
undefined
in their values could do so by preventing the compiler from recognizing the mapped type as homomorphic:The following works and is the most obvious: