我是Material UI和TypeScript的初学者。ps:如果我的问题格式不正确,我道歉,第一个问题是stackoverflow。


发布于 2021-01-14 02:27:27
type只接受文字字符串'light'和'dark'。这就是different to the general string type。
您提供给useState的初始值是文字类型,但是Typescript扩大了函数表达式的返回类型。换句话说,mode是string类型。
useState是一个generic function,您可以传入类型以防止类型扩大
const [mode, setMode] = useState<'light' | 'dark'>('dark')https://stackoverflow.com/questions/65706583
复制相似问题