3
My TypeScriptのバージョンが2.0.10
です。TypeScript + React:厳密なヌルチェックモードでオプションの小道具でdefaultPropsが機能しない
私はここにtrue
にstrictNullChecks
を設定するコンポーネント
import * as React from "react";
export interface HelloProps { list?: number[]; }
export class Hello extends React.Component<HelloProps, {}> {
static defaultProps = {
list: [1, 2, 3]
}
static propTypes = {
list: React.PropTypes.array,
title: React.PropTypes.string
};
render() {
let {list} = this.props
return (
<ul>
{
// error here: Object is possibly 'undefined'.
list.map(item => (
<li>{item}</li>
))
}
</ul>
)
}
}
活字体のコンパイラ設定ファイル
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"strictNullChecks": true
},
"include": [
"src/**/*"
]
}
注意。コンパイル出力:
ERROR in ./src/Hello.tsx
(19,21): error TS2532: Object is possibly 'undefined'.
しかし、リストのデフォルト値を設定しました。 TypeScriptのバグですか?