首页 > 解决方案 > 为什么我可以使用字符串文字创建一个对象,但如果泛型到位,我不能?

问题描述

我尝试使用字符串文字创建一个对象。


export type MyType<T extends string> = {
  propFromMyType: T;
};

export type TypeWithGenericLiteral<T extends string> = {
  [P in `${T}_with_literal`]: number;
};

如果我的创建函数本身不使用泛型类型,它可以正常工作:


const create = (t: MyType<"example">): TypeWithGenericLiteral<"example"> => {
    const prop = `${t.propFromMyType}_with_literal` as const;

    return {
        [prop]: 777
    };
}

但是如果 create 函数本身包含一个类型 T ,它就会分崩离析:


const create = <T extends string> (t: MyType<T>): TypeWithGenericLiteral<T> => {
    const prop = `${t.propFromMyType}_with_literal` as const;

    return {
        [prop]: 777
    };
} 

即使我将类型 T 更改为这样的特定文字,它也不起作用:


type action = "example"

export type MyType<T extends action> = {
  propFromMyType: T;
};

export type TypeWithGenericLiteral<T extends action> = {
  [P in `${T}_with_literal`]: number;
};


const create = <T extends action> (t: MyType<T>): TypeWithGenericLiteral<T> => {
    const prop = `${t.propFromMyType}_with_literal` as const;

    return {
        [prop]: 777
    };
} 

ts游乐场

标签: typescript

解决方案


这是因为无法知道函数将使用哪个类型参数调用。看看下面的例子:

const propFromMyType: 'example' | 'foo' = 'example';

create2({ propFromMyType })

预期的结果类型将是:

type ResultType = TypeWithGenericLiteral<'example' | 'foo'>
// { example_with_literal: number; foo_with_literal: number; }

推荐阅读