Typescript - 如何使值等于对象的键?
Typescript - How to make values to equal the keys of an object?
我想键入一个键值相同的对象:
const myObj = {
FOO: 'FOO',
BAR: 'BAR'
};
我尝试使用 myObj
和 key
:
设置 typeof
const actions: { [x: string]: x } = { // 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
set: 'set1'
}
const actions: { [x: string]: typeof x } = {
set: 'set1' // doesn't trigger
}
const actions: { [p: string]: keyof typeof actions } = {
set: 'set' // Type 'string' is not assignable to type 'never'.
}
type K = 'set' | 'set2';
const actions: { [p in K]: K } = { // triggers missing set2
set: 'set1' // doesn't trigger
}
有没有办法确保键和值始终匹配? TIA!
您可以通过创建一个辅助函数来实现这一点,该函数接受您的对象,然后通过生成额外的参数对其进行 TypeScript 检查,如果给定的对象没有通过我们相同的参数,这些参数将使您的代码失败 key/value法.
// basically, we create an extra parameter when the object is not valid
function createSamePairedObject<T extends Readonly<Record<PropertyKey, PropertyKey>>>(obj: T, ..._lockParams: T extends { [K in keyof T]: K } ? [] : ["INVALID_PAIRED_OBJECT"]): T {
return obj as any;
}
// PASS
const a = createSamePairedObject({ // { readonly a: "a" }
a: "a",
} as const);
// FAIL - "b" != "bb"
const b = createSamePairedObject({
a: "a",
b: "bb",
} as const);
// FAIL - not const object
const c = createSamePairedObject({
[5]: 5,
});
// PASS
const d = createSamePairedObject({
[5]: 5,
} as const);
玩了一会儿搞定了:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = { // note that I am using p instead of K from the question
set: 'set1'
}
使用此设置我得到以下错误:
TS2741: Property 'set2' is missing in type '{ set: "set"; }' but required in type '{ set: "set"; set2: "set2"; }'.
TS2322: Type '"set1"' is not assignable to type '"set"'.
以下是让它开心的原因:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = {
set: 'set',
set2: 'set2'
}
更新:如果您有已知的键列表,此方法有效,但如果您有未知的键列表或要将任何键与其值匹配,请遵循@sno2 的
我想键入一个键值相同的对象:
const myObj = {
FOO: 'FOO',
BAR: 'BAR'
};
我尝试使用 myObj
和 key
:
typeof
const actions: { [x: string]: x } = { // 'x' refers to a value, but is being used as a type here. Did you mean 'typeof x'?
set: 'set1'
}
const actions: { [x: string]: typeof x } = {
set: 'set1' // doesn't trigger
}
const actions: { [p: string]: keyof typeof actions } = {
set: 'set' // Type 'string' is not assignable to type 'never'.
}
type K = 'set' | 'set2';
const actions: { [p in K]: K } = { // triggers missing set2
set: 'set1' // doesn't trigger
}
有没有办法确保键和值始终匹配? TIA!
您可以通过创建一个辅助函数来实现这一点,该函数接受您的对象,然后通过生成额外的参数对其进行 TypeScript 检查,如果给定的对象没有通过我们相同的参数,这些参数将使您的代码失败 key/value法.
// basically, we create an extra parameter when the object is not valid
function createSamePairedObject<T extends Readonly<Record<PropertyKey, PropertyKey>>>(obj: T, ..._lockParams: T extends { [K in keyof T]: K } ? [] : ["INVALID_PAIRED_OBJECT"]): T {
return obj as any;
}
// PASS
const a = createSamePairedObject({ // { readonly a: "a" }
a: "a",
} as const);
// FAIL - "b" != "bb"
const b = createSamePairedObject({
a: "a",
b: "bb",
} as const);
// FAIL - not const object
const c = createSamePairedObject({
[5]: 5,
});
// PASS
const d = createSamePairedObject({
[5]: 5,
} as const);
玩了一会儿搞定了:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = { // note that I am using p instead of K from the question
set: 'set1'
}
使用此设置我得到以下错误:
TS2741: Property 'set2' is missing in type '{ set: "set"; }' but required in type '{ set: "set"; set2: "set2"; }'.
TS2322: Type '"set1"' is not assignable to type '"set"'.
以下是让它开心的原因:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = {
set: 'set',
set2: 'set2'
}
更新:如果您有已知的键列表,此方法有效,但如果您有未知的键列表或要将任何键与其值匹配,请遵循@sno2 的