如何将附加字符串与打字稿类型定义对象连接起来?

How to concat additional string with the typescript type definition object?

假设我有如下类型:

export enum configType {
  value = 'blah bala',
  value2 = 'blah bala',
  value3 = 'blah bala',
  value2 = 'blah bala',

}

现在我想用上面的枚举创建一个新类型如下:

export type analog = {
    [v + '_key' in configType]?: boolean;
}

这可能吗?

您可以使用 literal types the type utilities Record<Keys, Type> and Partial<Type> 来做到这一点。

TS Playground

enum ConfigType {
  Value = 'Value',
  Value2 = 'Value2',
  Value3 = 'Value3',
  Value4 = 'Value4',
}

type Analog = Partial<Record<`${ConfigType}_key`, boolean>>;

/*

type Analog = {
  Value_key?: boolean;
  Value2_key?: boolean;
  Value3_key?: boolean;
  Value4_key?: boolean;
}

*/