条件类型中类型推断的问题

问题描述 投票:0回答:1
type CtorParamsType<T> = T extends {
    new(...args: infer U);
} ? U : any;

class MyType {
    constructor(name: string, age: number) {

    }
}

type T1 = CtorParamsType<MyType> //any
type T2 = CtorParamsType<{ new(name: string, age: number); }> //[string, number]

在此示例中,我希望T1和T2具有相同的类型。为什么它们不同?

typescript types type-inference
1个回答
0
投票

您要查找的类型已经存在,称为ConstructorParameters

© www.soinside.com 2019 - 2024. All rights reserved.