#Narrowed union from multiple generic properties

13 messages · Page 1 of 1 (latest)

wet phoenix
#

The type of test is Test<string>, I am trying to get the generic value to be Test<'test1' | 'test2' | 'test3' | 'test4'>.

The example is just the minimal version I could provide, in my real use case the object "values" are classes with generics themselves, and I need to create one generic type out of all of the classes inside a parent class if that makes easier to understand.

#

Here is a more detailed playground link with classes:

tropic geodeBOT
#
lenovouser#1984

Preview:```ts
class Child<Value extends string> {
public value: Value;

public constructor({ value }: { value: Value }) {
    this.value = value;
}

}

class Parent<Keys extends readonly string[], const Values extends string> {
public keys: Keys;
public holder: { [Key in Keys[number]]: Child<Values> };

public constructor({ keys, holder }: { keys: Keys, holder: { [Key in Keys[number]]: Child<Values> } }) {
    this.keys = keys;
    this.holder = holder;
}

}

const test = new Parent({
...```

wet phoenix
#

The type of test is:

 Parent<readonly ["key1", "key2", "key3", "key4"], string>

When I would like it to be:

Parent<readonly ["key1", "key2", "key3", "key4"], 'value1' | 'value2' | 'value3' | 'value4'>
minor kindle
#

playground link is broken

tropic geodeBOT
#

@wet phoenix Here's a shortened URL of your playground link! You can remove the full link from your message.

lenovouser#1984

Preview:```ts
class Child<Value extends string> {
public value: Value;

public constructor({ value }: { value: Value }) {
    this.value = value;
}

}

class Parent<Keys extends readonly string[], const Values extends string> {
public keys: Keys;
public holder: { [Key in Keys[number]]: Child<Values> };
...```

tropic geodeBOT
#
Jakob#8686

Preview:```ts
class Child<Value extends string> {
public value: Value;

public constructor({ value }: { value: Value }) {
    this.value = value;
}

}

class Parent<Keys extends readonly string[], Key extends Keys[number], const Values extends string> {
public keys: Keys;
...```

polar pecan
#

works but adds another generic param, maybe theres another way

wet phoenix
#

@polar pecan that works, thank you so much!

#

Shall I close this or keep it open for a possible solution without a new generic?