小编use*_*034的帖子

A "const" assertion error typescript with string array

I'm trying to learn Typescript with React, but this error got in my way. I don't understand why is it even happening. Here it is. Creating an array of strings that looks like colors array down there. I want to create types based on values of that array ("white" | "red" | "blue").

const colors = ["white", "red", "blue"] as const;
type Colors= typeof colors[number];
Run Code Online (Sandbox Code Playgroud)

If I do it like this, it works, Colors has wanted types

const colorsArr = …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs typescript-typings

1
推荐指数
1
解决办法
1988
查看次数