- 在nx应用的组件中使用TypeScript时,可以在
script标签中加入lang="ts"来设置脚本语言。 - 通过API获取的数据
data默认类型是any,可以设置为自定义类型CurrentUser。 - 需先在
types目录下定义CurrentUser类型,包括id(number),name和token(都是string)。 - 在API请求时,将类型参数设置为
CurrentUser,使得data的类型变为CurrentUser。 - 若在组件模板中使用
CurrentUser并出现类型错误,可通过设置useState的类型参数为CurrentUser解决,确保编辑器知道数据的确切类型。