Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
11 views

Mapping Discriminated Union to function returning its values gives strange function union types [duplicate]

I'm trying to implement a generic component for my project that is able to use any object type and produce value parsing functions returning correct values. All seemed to work fine untill i tested it ...
Jovan Bulat's user avatar
0 votes
1 answer
27 views

Typescript: infer the type of an Object key from another key in the same Object

Let's say I have an API like this: function changePot(args: {newSize: number, newSoil: string}){ } function water(args: { quantity: number, useIntrants: boolean}){ } const API = { changePot, ...
Leo's user avatar
  • 25
1 vote
1 answer
33 views

Using string literal as a reference to named interface

I got multiple interfaces, say Int1, Int2 and so on interface Int1 {} interface Int2 {} And I want to create a registry, structured as const Registry = { "Int1": { "obj1": {}...
FFaFafy's user avatar
  • 63
1 vote
1 answer
50 views

Flatten mapped type to intersection removing the iterated union' keys used to create it

I am not sure how to better phrase the question but I hope the question title with the code below is self-explanatory, despite it might seem useless. EDIT reduced code to the bare minimum: type ...
kuus's user avatar
  • 485
0 votes
0 answers
42 views

Resolve Generic of Generic

I'm trying to create a reusable utility type in TypeScript that works with any map type conforming to a generic structure. Here's my basic setup: type SomeMap<T> = { [key: string]: T; }; I ...
Janek Eilts's user avatar
0 votes
2 answers
46 views

Can the type of a type parameter be inferred from the entries of an Array?

In the following example, is it possible for the type of P to be inferred from the entries in the props array, rather than having to define it twice? type A = { one: boolean two: number three: ...
james's user avatar
  • 4,513
0 votes
0 answers
48 views

Why does `[K in (keyof typeof SomeNumericEnum) as typeof SomeNumericEnum[K]]` result in the actual numerical index, rather than just `number`?

Given the following enum: enum Animals { CAT, DOG, PARROT, SHEEP, SALMON } The following mapped type: type AnimalsToMappedType = { [K in (keyof typeof Animals) as typeof Animals[K]]: K } ...
james's user avatar
  • 4,513
2 votes
1 answer
67 views

Why does adding parentheses remove 'readonly [x: number]: number;' from this type?

enum Animals { CAT, DOG, PARROT, SHEEP, SALMON } The following type: type AnimalsToMappedType = { [K in keyof typeof Animals]: K } Results in: type AnimalsToMappedType = { readonly [x: ...
james's user avatar
  • 4,513
0 votes
1 answer
36 views

How can I apply TS Conditional types, Mapped Types on this method?

I'm working with a TypeScript class that represents a form with various input types. Each input class has a getValue() method that returns different types based on the input. I need to create a type ...
chestacio's user avatar
2 votes
2 answers
61 views

How to generically set values of mapped type

See this following code example: type Properties = { item0: { item0: string }; item1: { item1: string }; item2: { item2: string }; item3: { item3: string }; item4: { item4: string }; }; ...
Dave Butler's user avatar
  • 1,750
0 votes
2 answers
28 views

In TypeScript how might one create a mapped, conditional type that removes properties of type string | null

Here's my final failed attempt before coming here for advice. What am I doing wrong pls ty? interface Something { id: string; name: string | null; } interface AnotherThing { id: string; name: ...
Joseph Beuys' Mum's user avatar
4 votes
0 answers
25 views

Do mapped types magically detect `null` and `undefined`? [duplicate]

If I can trust my vscode transpiler it tells me that for each of these types the result is the one I put in comments type keyOfNull = keyof null // never type keyOfundefined = keyof ...
Drax's user avatar
  • 13.2k
0 votes
2 answers
36 views

Create a RecursivePartializer

I have the following typescript code type Bar = { foo: string; prop: number; baz: { value: string; check: boolean; } } I want to create a mapped type such that type ...
robkuz's user avatar
  • 9,734
1 vote
1 answer
69 views

Is there a way to infer the keys of a mapped type when using Typescript?

I have a data structure that represents my business operations: const operations = { list: { request: "a", response: "b", }, get: { request: &...
Adam Arold's user avatar
  • 30.3k
1 vote
1 answer
194 views

Another TS 5.4 regression (i think).... how to go about fixing this type in 5.4

Recent Typescript changes (5.4.x, everything worked in 5.3.3) have made some regressions which caused a number of issues. The first one I posted about HERE. The fix proposed in that other post does ...
Dellirium's user avatar
  • 1,516
2 votes
1 answer
71 views

How to refactor long union type?

I want to create a React functional component which takes one of the predefined components as a prop and which logic depends on what component was passed in. So these are the possible names: enum ...
tnsaturday's user avatar
1 vote
1 answer
81 views

TypeScript mapped types: from list of both objects and strings to a key-value object

I'm trying to write a mapped type that takes a list which can contain both objects ("definitions") and strings. String values are limited to a set of strings that refer to existing hard ...
Even A. Rognlien's user avatar
3 votes
1 answer
642 views

Iteration through mapped type record with generic value

I have two records (record of options and record of instances) that have correspondence between key and value. I guarantee this correspondence by using mapped type with generic value. My task is to ...
Bunny Boss's user avatar
0 votes
1 answer
632 views

Automapper and Mapped type in nest js?

so I am creating a backend service using nest js I am following the dto pattern now I created a product dto like this: import { OmitType } from "@nestjs/mapped-types"; import { Product } ...
kikicoder's user avatar
  • 421
0 votes
0 answers
74 views

Mapped type not capable of inferring exact tuple (argument) element when generics explicitly set? Any workaround?

I am having trouble understanding how/why the mapped type is unable to handle explicitly typed arguments but is working perfectly fine with default types (and is able to infer exact args)? More ...
Dellirium's user avatar
  • 1,516
2 votes
1 answer
115 views

Is it possible to avoid the type assertion in this generic function body?

As a use case, I want to allow people to add an arbitrary function to an object, and call that function through another, using its name and parameters. // Example arbitrary function const sum = (a: ...
Mickaël Pham's user avatar
0 votes
0 answers
29 views

Constraining property names in an interface that uses declaration merging (possibly via extending?)

I am trying to use a typescript feature called "declaration merging" in order to allow my codebase to "declare" an interface in multiple files, which would then extend those key ...
Dellirium's user avatar
  • 1,516
1 vote
1 answer
42 views

Is it possible to constrain function arguments to a predefined type through a mapped type?

I am trying to extend some functionality of Redux (think similar to toolkit), and have hit a snag. From what I understand of the issue it has to do with covaraince/contravariance, but that subject is ...
Dellirium's user avatar
  • 1,516
0 votes
0 answers
51 views

Mapped type/using a TS variable for a multiple fields?

So, I would simplify a little bit. I work with Cognito, and I have the list of possibble attributes, I'll shorten it to: export type CognitoUserAttributes = { email?: string; name?: string; ...
ForceAwakenz's user avatar
2 votes
1 answer
189 views

Mapped type is not an array type

I apologise for the rather undescriptive title, I'm not sure how I should properly title this question. (Hopefully this isn't one of those XY problems...) I encountered this error whilst trying to ...
kwyntes's user avatar
  • 1,294
0 votes
1 answer
163 views

typescript how do i infer from mapping type

Hi I create a plugin and a function to callPlugin action. export class Plugin { actions = { 'join': ({ groupId }: { groupId: string }) => { }, 'addPoint': ({ point }: {...
Mr.Trieu's user avatar
  • 434
1 vote
1 answer
129 views

TypeScript Mapped Types - Is there a way to use interface value type as parameter of function type in mapped type?

I'm not sure how I might precisely describe my goal in words. Here is some code to explain what I would like to do: type validTypes = string | number type item = Record<string, validTypes> ...
octodecillion's user avatar
0 votes
0 answers
25 views

Type properties in discriminated union types

I want to create a discriminated union where all objects are of another type. say i have a type type PersonNames = 'john' | 'kate' type Person = { name: PersonNames, favorites: unknown // can be ...
gangqiu's user avatar
2 votes
1 answer
516 views

@nestjs/swagger and Angular in Nx monorepo: problem to share DTO

Recently, we started a new project with Angular and NestJS inside a monorepo Nx, and it works great. However, when we want to use @nestjs/swagger features inside a shared lib called "DTO", ...
Nxtivision's user avatar
1 vote
1 answer
153 views

How is java.util.LinkedList() an instance of MutableList?

How does the following code compiles without any errors? var linkedList: MutableList<Int> = java.util.LinkedList() when java.util.LinkedList class contract clearly doesn't implement the Kotlin ...
Alanpatchi's user avatar
  • 1,199
1 vote
0 answers
18 views

Capture deeply nested values using generics and mapped types

I'm trying to capture the value of a record that is nested in a property. However, I seem to only be able to capture the top-level types in the generic not nested ones. Here's an example that ...
Jonathan Sudelko's user avatar
2 votes
1 answer
83 views

Typescript mapped type with filtered keys

If I have a type like: type T = { a: string, _a: string, b: number, _c: boolean, /* ... any number of unknown properties ... */ }; Is there anyway I can define a new mapped type which has ...
Gershom Maes's user avatar
  • 7,967
0 votes
1 answer
18 views

Typing a function whose param is a key in a type, and whose `ReturnType` is the corresponding type for that key

I'm working in typescript. I have this type: type Mapping = { a: number, b: string, c: CustomType1, d: CustomType2 }; I want to define a separate type, named MapFn. This type defines ...
Gershom Maes's user avatar
  • 7,967
0 votes
1 answer
76 views

Typescript: How to remove properties of object/mapped type by value

Is there a way to remove some props of an object via a mapped type based on their value? So similar to Omit, but discriminating by value, instead of key. Given this example: type Q = {a: number, b: ...
MaximilianMairinger's user avatar
0 votes
3 answers
145 views

In TypeScript, how do I call a method by its name if this name is also an argument?

Is it possible to define Qux here in such a way as to avoid the error? I tried defining it as a mapped type, but with no luck. interface Qux { foo(stuff: string[], key: 'foo'): number; bar(stuff: ...
thorn0's user avatar
  • 10.2k
1 vote
1 answer
111 views

Why I'm getting error indexing in a conditional when mapping an object type

I'm adding a new property value:"4" to the object Foo, I have two approches in aaa and bbb. When hovering over them, both show the same expected result:{ name:string; value:"4";} ...
daego's user avatar
  • 309
2 votes
1 answer
47 views

Instance of generic type not assignable to instance of mapped type

Consider the following code: (playground) type Config = { a: {type: number, someOtherField: true}, b: {type: string, someOtherField: false}, } // "a" | "b" type Key = ...
Guillaume Brunerie's user avatar
0 votes
1 answer
80 views

TypeScript not recognizing mapped type with conditional types violation

I need help with these type definitions. TS is not showing me a warning but I believe I'm violating the type. type ElementType = "ShortText" | "Status" | "Number"; type ...
askrill's user avatar
  • 73
0 votes
1 answer
193 views

TypeScript: Inference Issue with Mapped Types and Discriminated Unions

I know that much has already been said about typing discriminated unions, but I could not find a solution for my particular case yet. Say I have the following code: type A = {label: 'a', options: {x: ...
Remirror's user avatar
  • 744
1 vote
0 answers
21 views

Why does TypeScript reject this generic function declaration using mapped types? [duplicate]

Using TS 5.1.3: $ npx tsc --version Version 5.1.3 I have the following function: const valueToObject = <K extends PropertyKey>(name: K) => <T>(v: T): { [P in K]: T } => ({ [...
Taro's user avatar
  • 1,532
1 vote
1 answer
72 views

Typescript, Type-Inferance: How to make Typescript be able to suggest the name of the props of an object?

Using RTK-query in React we've got a nice feature. Once we've defined the ApiSlice with it's endpoints RTK-query autogenerate hooks for us. Not only that, but Typescript is able to suggest what hooks ...
Emiliano Morghen's user avatar
0 votes
0 answers
58 views

In typescript, why use type can define but use interface will report an error

declares an interface of type A interface A { name: string; age: number; like: string[]; } I want to set all the values ​​in the A interface as readonly attributes interface B { [p in keyof A]:...
bughou's user avatar
  • 154
0 votes
1 answer
30 views

Exclude utility does not work in mapped types

I need exclude some literals for a mapped type: type ReformValidator = "required" | "minLength" | "maxLength"; interface FieldsValidator { [key: string]: { [...
Antonio González's user avatar
0 votes
1 answer
79 views

TypeScript - generic function collapses types from some generic interfaces to "unknown"

I have a function that merges the results of multiple API requests (if none of them has an error): mergeAPIResults({ data: 123 }, { data: 'test' }) // produces: { data: [ 123, 'test' ] } ...
blade's user avatar
  • 12.7k
1 vote
1 answer
42 views

Inferred second generic argument is not narrowed

I'm trying create function, that allows me specify some objects that extend type Record<string, Record<string, number>>) and defaults values for some of the properties for some of the ...
Niik's user avatar
  • 23
1 vote
0 answers
46 views

How Would I Type an Ordered Subset of a Generic Object up to a Specified Key?

Say I have a type test: type test = { a: number, b: number, c: number, d: number, } How would I create a generic type that will extract a subset of test up to a specified key in the ...
Dom C.'s user avatar
  • 11
1 vote
1 answer
606 views

Type like `Record<string, keyof T>` that guarantees each key in `keyof T` is used?

I make a simple example just to show a problem. There it is class Person{ public Name: string = ''; public Age: number = 0; } class Some { public StringToKey: Record<string, keyof ...
MikHail's user avatar
  • 13
1 vote
2 answers
998 views

Typescript: How do I create a mapped type from an union of object with single keys?

What I want to do is the following: Say I have a type: type UpdateUnion = {foo:number} | {bar:string} | {baz: {value1: number, value2: string}}; Is there a way to construct a mapped type ...
Salvo Miosi's user avatar
0 votes
0 answers
52 views

How does one write a type-assertion function to check whether all containers in an array are in a certain state?

I'm working with a library that models results to asynchronous operations in a way similar to the following: type Success<TData> = { status: 'success', data: TData }; type Loading = { status: '...
Jacques Visser's user avatar
2 votes
1 answer
71 views

Array of enum is switched to Array of enum member in typescript mapped type

I'm currently trying to create a mapped type that creates the angular form group type from a given model type. I've created a mapped type that checks if a given property is an array and then creates a ...
Limeray's user avatar
  • 23

1
2 3 4 5
8