Typescript 推断函数返回类型,知道一个属性的值

Typescript 推断函数返回类型,知道一个属性的值,typescript,typescript-generics,Typescript,Typescript Generics,有办法吗?我知道,我可以通过if/else或切换我的“type”属性来获得特定的接口。但我可以用函数(或方法)的返回值来做类似的事情吗 interface test1 { type: 'test1' } interface test2 { type: 'test2' } type unType = test1 | test2; //i know property "type"'s value //can i somehow use this information to i

有办法吗?我知道,我可以通过if/else或切换我的“type”属性来获得特定的接口。但我可以用函数(或方法)的返回值来做类似的事情吗

interface test1 {
    type: 'test1'
}

interface test2 {
    type: 'test2'
}

type unType = test1 | test2;

//i know property "type"'s value
//can i somehow use this information to infer specific type (test1 or test2)
function whichType<T>(typeValue): T {

    return null;
}

const tt1 = whichType<unType>('test1');// should be interface test1
const tt2 = whichType<unType>('test2');// should be interface test2
接口测试1{
类型:“test1”
}
接口测试2{
键入:“test2”
}
类型unType=test1 | test2;
//我知道属性“type”的值
//我是否可以使用此信息推断特定类型(test1或test2)
函数类型(typeValue):T{
返回null;
}
const tt1=whichType('test1');//应该是接口test1
const tt2=whichType('test2');//应该是接口test2

如果在调用
whichType
时确实使用了文本,则可以从类型的角度执行此操作,方法是使用函数重载:

interface test1 {
    type: 'test1'
}

interface test2 {
    type: 'test2'
}

type unType = test1 | test2;

function whichType(typeValue: 'test1'): test1;
function whichType(typeValue: 'test2'): test2;
function whichType(typeValue: string): unType {
    switch (typeValue) {
        case 'test1':
            return <test1>null;
        case 'test2':
            return <test2>null;
        default:
            throw new Error(`Unknown type ${typeValue}`);
    }
}

const tt1 = whichType('test1'); // tt1's type is test1
const tt2 = whichType('test2'); // tt2's type is test2
…然后处理你不知道类型的事实:-|

[在游乐场][2]


[2] :函数whichType(typeValue:string):取消键入

您可以按照TJ Crowder的建议使用重载,如果您只有几个接口,这可能是最好的解决方案,因为编写一个理解的代码很简单

更通用的解决方案是使用
Extract
条件类型根据传入的字符串提取类型:

interface test1 { type: 'test1' }

interface test2 { type: 'test2' }

type unType = test1 | test2;

function whichType<K extends unType['type']>(typeValue: K): Extract<unType, {type: K}> {
    return null!;
}

const tt1 = whichType('test1'); // test1
const tt2 = whichType('test2'); // test2
接口test1{type:'test1'}
接口test2{type:'test2'}
类型unType=test1 | test2;
函数whichType(typeValue:K):提取{
返回null!;
}
const tt1=whichType('test1');//测试1
const tt2=whichType('test2');//测试2
可以构建适用于任何联合的解决方案,但它要求您使用函数currying,因为typescript不支持部分类型参数推断:

function whichType<T extends { type: string}>() {
    return function <K extends T['type']>(typeValue: K): Extract<T, {type: K}> {
        return null!;
    }
}

const tt1 = whichType<unType>()('test1'); // test1
const tt2 = whichType<unType>()('test2'); // test2
函数whichType(){
返回函数(typeValue:K):提取{
返回null!;
}
}
常量tt1=whichType()('test1');//测试1
const tt2=whichType()('test2');//测试2
function whichType<T extends { type: string}>() {
    return function <K extends T['type']>(typeValue: K): Extract<T, {type: K}> {
        return null!;
    }
}

const tt1 = whichType<unType>()('test1'); // test1
const tt2 = whichType<unType>()('test2'); // test2