【发布时间】:2022-10-07 14:51:25
【问题描述】:
鉴于此代码
export interface ICollectionService {
get(id: string): Promise<Collection | null>;
}
const collection = await collectionService.get(collectionAddress);
现在我在 IDE 中显示的 collection 变量是 Collection 类型
不像我预期的那样Collection | null。
不确定这是否与 eslint 有关?
这是我的.eslintrc.js
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [\'eslint:recommended\', \'plugin:@typescript-eslint/recommended\'],
overrides: [],
parser: \'@typescript-eslint/parser\',
parserOptions: {
ecmaVersion: \'latest\',
sourceType: \'module\'
},
plugins: [\'@typescript-eslint\'],
rules: {}
};
-
你的 tsconfig 是什么?如果您不是在严格模式下每个类型都可以为空,那么
Collection | null与Collection是一样的。 -
在 tsconfig/eslintric.compilerOptions 中添加 \"strictNullChecks\": true 。
标签: typescript visual-studio-code eslint