Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapped Types: removing optional modifier

Given this code:

interface Foo{
  one?: string;
  two?: string;
}

type Foo2 = {
  [P in keyof Foo]: number;
}

I would expect the type of Foo2 to be { one: number; two: number; } However, instead it seems to keep the optional modifier { one?: number; two?: number; }

Is it possible to remove the optional modifier when using mapped types?

like image 206
NSjonas Avatar asked Apr 04 '18 15:04

NSjonas


People also ask

How do I get rid of optional in TypeScript?

til/remove readonly and optional parameters in typescript We are adding the modifier. Similarly, we can also remove modifiers by explicitly adding a minus sign, - , in front of it, i.e., -readonly .

What does ?: Mean in TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

Should I use type or interface TypeScript?

Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.


2 Answers

In Typescript 2.8 you can explicitly eliminate the modifier:

type Foo2 = {
  [P in keyof Foo]-?: number;
}

Or use the Required type that is built into newer versions.

If you are using an older version you can use this workaround:

type Helper<T, TNames extends string> = { [P in TNames]: (T & { [name: string]: never })[P] };
type Foo3 = Helper<Foo, keyof Foo>;
like image 96
Titian Cernicova-Dragomir Avatar answered Sep 22 '22 11:09

Titian Cernicova-Dragomir


You can use Required<T> as an alternative to -?

interface Foo {
  one?: string;
  two?: string;
}

type Foo2 = {
  [P in keyof Required<Foo>]: number;
};
like image 22
iarroyo Avatar answered Sep 18 '22 11:09

iarroyo