admin管理员组文章数量:1352882
I want to type an object where the key and value are the same:
const myObj = {
FOO: 'FOO',
BAR: 'BAR'
};
I tried setting up typeof
with myObj
, and with the key
:
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
}
Is there a way I can make sure the key and value match all the time? TIA!
I want to type an object where the key and value are the same:
const myObj = {
FOO: 'FOO',
BAR: 'BAR'
};
I tried setting up typeof
with myObj
, and with the key
:
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
}
Is there a way I can make sure the key and value match all the time? TIA!
Share Improve this question asked Jan 9, 2022 at 17:22 Gopikrishna SGopikrishna S 2,4494 gold badges29 silver badges40 bronze badges 2-
There is the
Set
object which only has key property which you also use as a value – Berkays Commented Jan 9, 2022 at 17:24 - @Berkays Can you please provide an example? I haven't used Set in typescript before.. – Gopikrishna S Commented Jan 9, 2022 at 17:32
2 Answers
Reset to default 9I got it after playing with for a while:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = { // note that I am using p instead of K from the question
set: 'set1'
}
With this setup I get the following errors:
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"'.
And following is what makes it happy:
type K = 'set' | 'set2';
const actions: { [p in K]: p } = {
set: 'set',
set2: 'set2'
}
UPDATE: This works if you have a known list of keys, but if you have an unknown list of keys or to match any key to its value, please follow @sno2 's answer
You can do this by creating a helper function that takes in your object then does a TypeScript check on it by generating extra parameters that would make your code fail if the given object does not pass our same key/value law.
// 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);
TypeScript Playground Link
本文标签: javascriptTypescriptHow to make values to equal the keys of an objectStack Overflow
版权声明:本文标题:javascript - Typescript - How to make values to equal the keys of an object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743917134a2561413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论