mapped-types Questions

4

Solved

Typescript allows you to remove 'optionability' (?) with the -? mapped type modifier, made easily available to use with the Required<T> type. type Required<T> = { [P in keyof T]-?: T[...
Konyn asked 2/8, 2019 at 10:9

1

Solved

I'm trying to model my data in Typescript in the following manner. Main focus is the type MessageHandler that maps the type in a message with a callback that accepts that message. I followed the ha...

3

Solved

I've been trying to create a type that consists of the keys of type T whose values are strings. In pseudocode it would be keyof T where T[P] is a string. The only way I can think of doing this is ...

1

Solved

I have an object. let obj1: A; /* type A = { property1: any; property2: any; } */ I know that the values in the object are all strings, but I don't want to forcefully typecast. // I don't want t...
Morville asked 9/1, 2023 at 6:37

2

Solved

enum AllowedFruits { Apple = 'APPLE', Banana = 'BANANA', Pear = 'PEAR' } const allowedFruits: AllowedFruits[] = [ AllowedFruits.Apple, AllowedFruits.Banana, AllowedFruits.Pear ] What I want t...
Trashy asked 19/5, 2022 at 8:11

2

Solved

I can create a type with different types for getter/setters like so: type Example = { get prop(): number; set prop(value: any); } I can also map keys to different values: type Mapped<T extend...
Perihelion asked 2/8, 2022 at 6:15

4

Solved

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 i...
Eec asked 4/4, 2018 at 15:52

5

Solved

Any ideas as to how might apply TypeScript's Partial mapped type to an interface recursively, at the same time not breaking any keys with array return types? The following approaches have not been...
Distrustful asked 28/7, 2017 at 11:22

1

Solved

please forgive me the slightly longish title. given the following type type A = { foo: string; bar: number; baz: boolean; } I like to create a new "partial" type B type B = Partial&lt...
Salinas asked 19/9, 2021 at 19:53

1

Solved

I'm attempting to make a mapped type which takes all properties with a certain type (in my case, arrays) and makes them optional. But any other properties (non-arrays) are left untouched. I know th...
Luciennelucier asked 22/7, 2021 at 22:45

5

Solved

Given an interface (from an existing .d.ts file that can't be changed): interface Foo { [key: string]: any; bar(): void; } Is there a way to use mapped types (or another method) to derive a new ...
Samellasameness asked 22/7, 2018 at 12:35

1

Solved

Dynamic object key without widening to { [key: string]: V } ? I'm trying to create a Typescript function to generate an object with a dynamic key whose name is provided in the function signature, w...
Zoara asked 23/6, 2021 at 1:17

2

Solved

Is it possible to make a mapped type property optional conditionally? Consider this type type Definition { name: string, defaultImplementation?: ImplementationType } and a record of them: type D...
Hindsight asked 3/4, 2021 at 5:40

2

From this data structure : const properties = [ { name: 'name', type: '' }, { name: 'age', type: 0 }, { name: 'sex', type: ['m', 'f'] as const }, { name: 'username', type: '' } ] I am trying t...
Errol asked 5/3, 2021 at 23:9

5

Solved

I would like to define a TS function to transpose an object to array for instance: const original = { value: [1, 2, 3], label: ["one", "two", "three"] } const tr...
Theocracy asked 21/2, 2021 at 13:52

2

Solved

When trying to use mapped types with interface, i get a weird error - which makes me think its not possible to use them together at all.. See identical type and interface declarations: type AllWo...
Jonell asked 14/3, 2018 at 17:56

1

Solved

I have an object that can have an n number of properties, each one the same but with their n value in the name. Example: const obj = { 'data-element-0': 'something', 'data-element-1': 'something ...
Cicely asked 15/12, 2020 at 14:1

2

Solved

How can I take the type { 'k': number, [s: string]: any } and abstract over 'k' and number? I would like to have a type alias T such that T<'k', number> gives the said type. Consider the fol...
Efrenefron asked 23/11, 2020 at 13:23

2

Solved

Suppose having a container type with array properties of unknown/generated types T1, T2, etc. (short T*): interface MultiContainer { Item1: T1[]; Item2: T2[]; ... } Is it possible to derive the...
Reisfield asked 30/6, 2017 at 13:58

1

Solved

I am having no luck understanding why the code below functions as it does: type MapOverString<T extends string> = { [K in T]: K }; type IfStringMapOverIt<T> = T extends string ? MapOve...
Interfaith asked 24/10, 2020 at 9:25

2

Here is the code class A { x = 0; y = 0; visible = false; render() { } } type RemoveProperties<T> = { readonly [P in keyof T]: T[P] extends Function ? T[P] : never//; }; var a = n...
Changeling asked 21/3, 2018 at 3:19

1

Solved

I'm trying to define helper types for determining the type of nested object values, whilst also considering any optional parent keys, e.g. in structures like these (or deeper): type Foo = { a: { b...
Lyndseylyndsie asked 26/3, 2020 at 14:22

1

Solved

In TypeScript, private attributes are considered part of the shape (or interface) of a type. class Person { constructor(private name: string, public age: number) { } } const p: Person = { age: 42...
Sanmicheli asked 25/2, 2020 at 7:35

0

Following on the heels of Mapped types in Scala Is there a way to mimic Pick, Exclude, Diff and etc. from Typescript in Scala? Using the a similar example as above: case class Person(name: Strin...
Carmelo asked 14/1, 2020 at 4:7

1

Solved

How to map a tuple generic type to a union type? type NeededUnionType<T> = T[keyof T]; // Includes all the Array properties values const value: NeededUnionType<[7, string]> = 2; // Th...

© 2022 - 2025 — McMap. All rights reserved.