typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here

本文介绍如何在TypeScript配置文件中设置目标为ES2015,包括具体的配置选项,如编译目标、模块类型、源代码映射等,并指出了排除和包含的文件路径。

将ts配置文件中 "target": "es2015"

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": false,
        "noImplicitAny": false,
        "module": "commonjs",
        "target": "es2015"
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

转载于:https://www.cnblogs.com/douglasvegas/p/7154917.html

在 React 和 Ant Design 的 Table 组件里,出现 `'FixedType' only refers to a type, but is being used as a value here` 错误,往往是因为把类型当作值来使用了。`FixedType` 是类型定义,不能当作值直接用在代码里。以下是几种解决办法: #### 检查代码中使用 `FixedType` 的地方 要保证没有将类型当作值使用。比如,若代码里有类似下面这样错误的用法: ```typescript import { Table } from 'antd'; import type { FixedType } from 'antd/es/table/interface'; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', fixed: FixedType.left, // 错误:FixedType 是类型,不能当作值使用 }, ]; ``` 可以把 `FixedType` 去掉,直接使用合法的值: ```typescript import { Table } from 'antd'; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', fixed: 'left', // 正确:使用合法的值 }, ]; ``` #### 若需要类型检查 可以使用类型注解来确保使用的值符合 `FixedType` 的定义: ```typescript import { Table } from 'antd'; import type { FixedType } from 'antd/es/table/interface'; const columns: { title: string; dataIndex: string; key: string; fixed?: FixedType; }[] = [ { title: 'Name', dataIndex: 'name', key: 'name', fixed: 'left' as FixedType, // 使用类型断言确保值符合 FixedType }, ]; ``` #### 确保导入正确 要保证 `FixedType` 导入正确,避免导入错误的类型或者重复定义类型。 ### 示例代码 ```typescript import React from 'react'; import { Table } from 'antd'; import type { FixedType } from 'antd/es/table/interface'; interface User { key: string; name: string; age: number; } const data: User[] = [ { key: '1', name: 'John', age: 25, }, ]; const columns: { title: string; dataIndex: string; key: string; fixed?: FixedType; }[] = [ { title: 'Name', dataIndex: 'name', key: 'name', fixed: 'left' as FixedType, }, { title: 'Age', dataIndex: 'age', key: 'age', }, ]; const App: React.FC = () => { return <Table columns={columns} dataSource={data} />; }; export default App; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值