返回类型
返回类型
对象文字只能指定已知属性
.
// Type '{ status: string; bar: string; }' is not assignable to type '{ status: string; }'.
// Object literal may only specify known properties, and 'bar' does not exist in type '{ status: string; }'.(2322)
const foo = function(): {status: string} {
return {
status: '200 OK',
bar: 'baz', // Problem
}
}
,在从返回的对象中忽略已知属性时,我们将收到一个类型错误,指出缺少已知属性:
// Type '() => { baz: string; }' is not assignable to type '() => { status: string; }'.
// Property 'status' is missing in type '{ bar: string; }' but required in type '{ status: string; }'.(2322)
const foo: () => {status: string} = function () {
return {
// status: '200 OK'
bar: 'baz',
}
}
但是,在定义
函数类型
不
const foo: () => {status: string} = function () {
return {
status: '200 OK',
bar: 'baz', // No problem
}
}
-
为什么最后一个示例没有引发类型错误?
-
有没有办法使用
函数类型
?
See in playground