2016-11-24 19 views
3

My TypeScriptのバージョンが2.0.10です。TypeScript + React:厳密なヌルチェックモードでオプションの小道具でdefaultPropsが機能しない

私はここにtruestrictNullChecksを設定するコンポーネント

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のバグですか?

答えて

1

役立つはずlistの後ろに感嘆符!を追加:

list!.map(item => (
    <li>{item}</li> 
)) 
関連する問題