Issue
I'd like to use this array with a union type, but TS rightly assumes that its type is string: "Argument of type 'string' is not assignable to parameter of type '"a" | "b"'."
function doSomething(value: "a" | "b"){}
["a", "b"].map(e => doSomething(e));
Is there a ways in which I can define the types of the array elements? If not, is there another way to solve this problem? I do not want to cast it in map().
Solution
The correct answer is using a const assertion:
function doSomething(value: "a" | "b"){}
(["a", "b"] as const).map(e => doSomething(e));
Answered By - julian.a
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.