1

Как указать ts брать имя свойства интерфейса из экземпляра класса, который нужно найти по ключу из значения другого свойства?

class A {aaa: number = 1;}
class B {bbb: number = 2;}
class C {ccc: number = 3;}

interface I {
    qwerty: A;
    abcde: B;
    zzz: C;
}

interface Test<T extends keyof I, K = I[T]> {
    test1: T;
    [M: keyof K]: K[typeof M]; // typeof K[M] ???
}

function load<T extends keyof I>(test: Test<T>): void {}

load({
    test1: 'abcde',
    bbb: 50, // Argument of type '{ test1: "abcde"; bbb: number; }' is not assignable to parameter of type 'Test<"abcde", B>'. Object literal may only specify known properties, and 'bbb' does not exist in type 'Test<"abcde", B>'
});

2 ответа 2

2

Вместо интерфейса нужно использовать mapped types

type Test<T extends keyof I, K = I[T]> = {
    test1: T;
} & {
    [M in keyof K]: K[M]; 
}

В этом случае к обязательному полю test1 добавляются все поля соответствующего класса

Playground Link

2
  • K можно не выносить в generic.
    – Qwertiy
    5 окт 2021 в 18:11
  • @Qwertiy, можно
    – Grundy
    5 окт 2021 в 18:28
-1

Воспользуйтесь type guards, например:

class A {
  aaa = 1;
  kind: 'typeA' = 'typeA';
}
class B {
  bbb = 2;
  kind: 'typeB' = 'typeB';
}

function load(test: A | B) {
  if (test.kind === 'typeA') {
    console.log(test.aaa);
  } else {
    console.log(test.bbb);
  }
}

load({
  aaa: 2,
  kind: 'typeA',
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.