我使用TypeScript 5.2版本,我有以下设置:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"noImplicitAny": true,
"esModuleInterop": true,
"noEmitOnError": true,
"moduleDetection": "force",
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"lib": [
"esnext",
"esnext.disposable", // Value is not accepted.
"dom"
]
}
}
和以下文件:
const getFile = () => {
return {
[Symbol.dispose]: () => {
console.log('x!')
}
}
}
console.clear()
using(const file = getFile()) { // ERROR: Cannot find name 'using'.ts(2304)
// do something with file
}
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
但是我不能使用using
关键字,得到错误://无法找到名称'using'. ts(2304)
似乎"esnext.disposable"
不被接受。
如何设置TypeScript使用Disposable?谢谢
1条答案
按热度按时间tag5nh1u1#
你的语法是错误的,你还需要polyfill
Symbol.dispose
才能让它在当前的JS环境中工作。以下内容将起作用:TS Playground链接
TS博客文章解释新语法