首页 > 解决方案 > 如何在打字稿中重建文字字符串键

问题描述

我们知道我们可以用来keyof从对象中提取密钥。

const a = { a1: 1, b2: 1, c3: 1 };

type AI = {
    [key in keyof typeof a]: boolean;
}

在这里我们得到了新类型

type AI = {
    a1: boolean;
    b2: boolean;
    c3: boolean;
}

但是如何重命名密钥,有人知道吗?</p>

type AI = {
    [key in ( 'prefix_' + keyof typeof a)]: boolean;// wrong
}

// here I want to get type
type AI = {
    prefix_a1: boolean;
    prefix_b2: boolean;
    prefix_c3: boolean;
}

标签: typescripttypescript-typings

解决方案


推荐阅读