Linked Questions
43 questions linked to/from Generic type to get enum keys as union string in typescript?
1 vote
1 answer
1k views
Distribution of enum types in template strings [duplicate]
enum Foo { 'Foo' = 'A', 'Bar' = 'B' } type Value = `${Foo}` // 'A' | 'B' I'm wondering why the enum type triggers the autodispatch feature in the template string type? Does anyone know what's ...
0 votes
0 answers
65 views
What is the outcome of "typeof Enum" inTypescript? [duplicate]
I know that an enum in Typescript compiles down to an object for runtime. I wanted to merge two enums enum EnumOne { ... }; enum EnumTwo { ... } with Object.assign(). That works. In doing so, ...
1 vote
0 answers
44 views
Generic type guarding with typescript [duplicate]
abstract class A { } class B extends A { public static readonly TYPE = "A"; } interface forValue<T extends { TYPE: string }> { type: T["TYPE"] } The above works perfectly fine, now I ...
1 vote
0 answers
39 views
Define type based on another type by omitting array of keys [duplicate]
I've got the following type: type MyType = { a: string; b: number; c: boolean; } and I'd like to define a type that's missing the fields listed in the string array const keysToDrop = ['a',...
1 vote
0 answers
30 views
How to Omit values for type definition from a set of values from Enum [duplicate]
I defined two types from Enum picking up values goodValue?: `${ValueOf<typeof SampleEnum>}`; goodValueWithExclude?: `${ExcludeValue<typeof SampleEnum, 'ONE'>}`; export enum SampleEnum { ...
122 votes
4 answers
94k views
How to define string literal union type from constants in Typescript
I know I can define string union types to restrict variables to one of the possible string values: type MyType = 'first' | 'second' let myVar:MyType = 'first' I need to construct a type like that ...
31 votes
2 answers
80k views
Map array of object to another array with different object in typescript
If i have something like array1: string[] = ['foo', 'bar']; and an interface like export interface MyObject { name: string; } how can I map array1 to another array of type MyObject? array2 : ...
7 votes
1 answer
8k views
Combine two types into an interface elegantly in Typescript
My goal I have a string enum E and an interface I with identical sets of keys. I want to construct a new mapped type. For each shared key k, it should use the enum value E.k as a property name. The ...
5 votes
5 answers
5k views
How to ensure a Typescript string enum has the same key and value
I want to make a generic type that checks the following on an enum: all fields are strings all of the values are equal to their own keys So in which case, the following enums would be considered "...
10 votes
2 answers
9k views
Property 'runStaticMethod' is a static member of type 'DemoClass'
I am getting the following error when I build the following typescript code. Property 'runStaticMethod' is a static member of type 'DemoClass' Typescript Code: export class Main { constructor(...
3 votes
1 answer
10k views
Passing generic enum type as a parameter to a function in Typescript
I have two string enums that have the same variants, but have different string values. They get passed to the same initial function, which accepts the entire enum (which I think I can do using typeof, ...
2 votes
1 answer
5k views
Typescript - How to get the value of enum type in type definition
I'm trying to define a type based on the value of an enum. enum E { A = "apple", B = "banana", C = "cherries" } // type EnumKey = "A" | "B&...
2 votes
1 answer
3k views
How do I a union type of string values from an enum? [duplicate]
I have an enum like this enum A { Car = "car", Bike = "bike", Truck = "truck" } and I want to get a type that is car | bike | truck I know that keof typeof A can ...
2 votes
1 answer
3k views
Typescript - expect type which extends class
I need to create a function which would expect any type of class which extends another specific class (type, not an instance) Class A{} foo(b: typeof 'extends A') Class B extends A{} Class C {} ...
3 votes
1 answer
3k views
How to add "newable" constraint for generic parameter in typescript?
I already know how to add "newable" (i.e. has constructor) constraint for function arguments (like argument for foo function below), but same techique doesn't apply to generic type parameters. Why ...