我用了一段时间的打字稿,有点喜欢它,除了编译可能要花很长时间。
今天,我发现在vscode中,我可以编写js代码,并使用jsdoc添加一些类型标记,并在代码的第一行添加//@ts-check
。
但是,我对代码的信心不如使用带有--strictNullCheck标志的tsc那样。
所以,我的问题是,是否可以在js代码中使用strictNullCheck运行//@ts-check
?
这是一个我没有得到编译错误的例子:
// type.js
// @ts-check
/**
* @typedef Foo
* @property {string} foo
* @property {string} [bar]
*/
/** @type {Foo} */
const a = { bar: 'test', foo: undefined }; // <- should be compile error
// index.js
import * as T from './type';
/** @type {T.Foo} */
const a = {}; // <- should be compile error
a.foo.replace // <- will be runtime error.