admin管理员组文章数量:1425740
I want to implement a switch-like function similar to the when
operator in Kotlin in Typescript.
Example usage:
const a = 30;
const b = 10;
const result = when(
{[a < b]: 'somethin'},
{[a > b]: 'somethin else'},
{[a >= b]: 'somethin else 2'},
)
>> result == 'something else'
It returns the value of the first case with condition evaluated to true.
I tried to do something like this:
type Case<T> = { [key: boolean]: T };
function when<T>(...cases: Case<T>[]) {
const index = cases.findIndex(c => c.hasOwnProperty(true));
return index >= 0 ? cases[index][true] : undefined;
}
but the TS piler is plaining with An index signature parameter type must be either 'string' or 'number'.ts(1023)
.
Also, when I try to do something like this:
const foo = {[a > b]: 'something'};
TS is again erroring out with A puted property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
This can easily be done in pure JS since the boolean result in the puted property is automatically coerced (converted to) string.
I could not find any way of doing this online so a settled with doing this instead:
function when<T>(...cases: [boolean, T][]) {
const index = cases.findIndex(([condition, _]) => condition);
return index >= 0 ? cases[index][1] : undefined;
}
when(
[a < b, 'somethin'],
[a > b, 'somethin else'],
[a >= b, 'somethin else 2'],
)
This is fine, but I find the syntax of the first example more pleasing to look at.
Am I missing something or is this limitation of the current spec?
I want to implement a switch-like function similar to the when
operator in Kotlin in Typescript.
Example usage:
const a = 30;
const b = 10;
const result = when(
{[a < b]: 'somethin'},
{[a > b]: 'somethin else'},
{[a >= b]: 'somethin else 2'},
)
>> result == 'something else'
It returns the value of the first case with condition evaluated to true.
I tried to do something like this:
type Case<T> = { [key: boolean]: T };
function when<T>(...cases: Case<T>[]) {
const index = cases.findIndex(c => c.hasOwnProperty(true));
return index >= 0 ? cases[index][true] : undefined;
}
but the TS piler is plaining with An index signature parameter type must be either 'string' or 'number'.ts(1023)
.
Also, when I try to do something like this:
const foo = {[a > b]: 'something'};
TS is again erroring out with A puted property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
This can easily be done in pure JS since the boolean result in the puted property is automatically coerced (converted to) string.
I could not find any way of doing this online so a settled with doing this instead:
function when<T>(...cases: [boolean, T][]) {
const index = cases.findIndex(([condition, _]) => condition);
return index >= 0 ? cases[index][1] : undefined;
}
when(
[a < b, 'somethin'],
[a > b, 'somethin else'],
[a >= b, 'somethin else 2'],
)
This is fine, but I find the syntax of the first example more pleasing to look at.
Am I missing something or is this limitation of the current spec?
Share Improve this question edited Aug 18, 2023 at 22:52 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Mar 17, 2020 at 15:30 Iliya ZhechevIliya Zhechev 7111 gold badge6 silver badges6 bronze badges2 Answers
Reset to default 0TypeScript has strong typing, you should not expect such coercion. Object keys in JavaScript must be string or number. That is why TypeScript is asking you to not use boolean values. You could try the following solution:
type Case<T> = { [key in "true" | "false"]: T };
However, you need to be aware that you cannot have two identical keys: see the example below:
let sampleObject = {
"true": 'something',
"false": 'something else',
"false": 'something else 2'
}
console.log(sampleObject);
// will print { true: "something", false: "something else 2" }
// note that the second value is not available because
// you cannot have 2 "false" keys
```
It has to be string:
{[(a < b).toString()]: 'somethin'},
本文标签: javascriptBoolean value as object key type in TypeScriptStack Overflow
版权声明:本文标题:javascript - Boolean value as object key type in TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449586a2658823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论