【发布时间】:2021-08-27 22:39:10
【问题描述】:
<MyCustomField
type={props.type}
MyCustomField 的type 类型定义:
type?: string;
props.type的类型定义:
type?: string;
感觉就像我不小心打开了某些设置。我的 tsconfig:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": false,
"skipLibCheck": true,
"noImplicitAny": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
},
"include": [
"src"
]
}
编辑:另外,它编译得很好:
只是 VSCode Intellisense 不喜欢而已。
编辑 2:
if (!clone[index].options) throw Error(`Data at index ${index}, ${optionIndex} doesn\'t have options`);
if (type === FieldDataTypeEnum.RadioButton) {
clone[index].options.forEach(o => o.checked = false);
} else {
clone[index].options[optionIndex].checked = true;
}
这也给了我一个错误,说 clone[index].options 可能是未定义的,即使 if 语句应该忽略这一点:
但仍然可以正常编译。
【问题讨论】:
-
负责的设置大概是
"strict": true。我对 React 了解不多,但作为一种猜测,我会说 React 可能将?:解释为仅在属性不存在时才允许未定义。 -
您能否在沙盒中重现此问题,codesandbox.io ?
-
@AjeetShah 不,就像我说的,这似乎是一个智能感知问题
-
在这种情况下,您应该尝试更新您的 VSCode,关闭它并重新打开它。
-
@AjeetShah 我有。
标签: reactjs typescript visual-studio-code javascript-intellisense