question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Removing optional modifier also removes `undefined` from value type

See original GitHub issue

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.opt type should support undefined.

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 support undefined.

Playground Link: https://www.typescriptlang.org/play/#src=%2F%2F A *self-contained* demonstration of the problem follows... %2F%2F Test this by running `tsc` on the command-line%2C rather than through another build tool such as Gulp%2C Webpack%2C etc. interface OptClass { opt%3F%3A number%3B } type NonPartialIsh %3D {[K in keyof OptClass]-%3F%3A OptClass[K] | undefined}%3B const test %3D {opt%3A undefined}%3B verify<NonPartialIsh>(test)%3B %2F%2F should NOT be error%2C but shows error function verify<T>(a%3A T) { }

Related Issues:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
jcalzcommented, Apr 19, 2019

Unfortunately missing and undefined aren’t consistently two different things in TypeScript; see #13195.

Relevant documentation on the behavior of -?:

Note that in --strictNullChecks mode, when a homomorphic mapped type removes a ? modifier from a property in the underlying type it also removes undefined from the type of that property

But in

{[K in keyof T]-?: Foo<T[K]>}

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:

type NullablyRequired<T> = { [P in (keyof T & keyof any)]: T[P] }

type Test = NullablyRequired<{a?: string, b: number}>
// type Test = {a: string | undefined, b: number}
5reactions
gordonmleighcommented, Jun 28, 2021

The following works and is the most obvious:

export type NonPartial<T> = { [K in keyof Required<T>]: T[K] };
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found