type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

如何获取一个类继承的类型的泛型参数的类型

Open hbcraft opened this issue 1 year ago • 0 comments

type IA<T extends object, P = unknown> = {
  x: P;
} & T;

interface IB {
  a: number;
}

class A implements IA<IB, boolean> {
  x = true;
  a = 0;
  b = '1';
}

function fn(a: A, b: any) {
  // 如何将b推断为IB
}

fn(new A());

我尝试使用泛型获取IB,但获取到的类型是A

function fn1<T extends object, P>(a: IA<T, P>, b: T) {
  console.log(a, b);
}

fn1(new A(), {
  a: 1,
  b: '2',
  x: true,
});

他包含了A类上所有的属性

hbcraft avatar Apr 26 '24 03:04 hbcraft