cli*_*ong 5 typescript typescript2.0
我的TypeScript版本是2.0.10。
组件
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>
)
}
}
Run Code Online (Sandbox Code Playgroud)
TypeScript编译器配置文件
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"strictNullChecks": true
},
"include": [
"src/**/*"
]
}
Run Code Online (Sandbox Code Playgroud)
请注意,我设置strictNullChecks为true此处。并编译输出:
ERROR in ./src/Hello.tsx
(19,21): error TS2532: Object is possibly 'undefined'.
Run Code Online (Sandbox Code Playgroud)
但是我已经为列表设置了默认值。它是TypeScript错误吗?
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |