admin管理员组文章数量:1318156
Typescript will check unknown properties:
(below will have error: "'c' does not exist in type..."
)
interface IReturnVal {
a: boolean;
b: boolean;
}
function oh(): IReturnVal {
return {
a: true,
b: false,
c: 1, // Type '{ a: true; b: false; c: number; }' is not assignable to type 'IReturnVal'. Object literal may only specify known properties, and 'c' does not exist in type 'IReturnVal'.ts(2322)
};
}
oh();
However if functions returning Promise, Typescript will only check for missing properties
(below will have error: "Property 'a' is missing in type..."
)
interface IReturnVal {
a: boolean;
b: boolean;
}
function ok(): Promise<IReturnVal> {
// Property 'a' is missing in type ....
return Promise.resolve({
b: false,
});
}
ok();
But not checking unknown properties.
(below will not have any error or warning)
expect: Have error"Object literal may only specify known properties..."
interface IReturnVal {
a: boolean;
b: boolean;
}
function ok(): Promise<IReturnVal> {
return Promise.resolve({
a: true,
b: false,
c: 1, // will not cause any error or warning
});
}
ok();
Is this the expect behavior or I'm missing something in Typescript setting?
Typescript will check unknown properties:
(below will have error: "'c' does not exist in type..."
)
interface IReturnVal {
a: boolean;
b: boolean;
}
function oh(): IReturnVal {
return {
a: true,
b: false,
c: 1, // Type '{ a: true; b: false; c: number; }' is not assignable to type 'IReturnVal'. Object literal may only specify known properties, and 'c' does not exist in type 'IReturnVal'.ts(2322)
};
}
oh();
However if functions returning Promise, Typescript will only check for missing properties
(below will have error: "Property 'a' is missing in type..."
)
interface IReturnVal {
a: boolean;
b: boolean;
}
function ok(): Promise<IReturnVal> {
// Property 'a' is missing in type ....
return Promise.resolve({
b: false,
});
}
ok();
But not checking unknown properties.
(below will not have any error or warning)
expect: Have error"Object literal may only specify known properties..."
interface IReturnVal {
a: boolean;
b: boolean;
}
function ok(): Promise<IReturnVal> {
return Promise.resolve({
a: true,
b: false,
c: 1, // will not cause any error or warning
});
}
ok();
Is this the expect behavior or I'm missing something in Typescript setting?
Share Improve this question edited Oct 12, 2021 at 10:45 Timothy Lee asked Oct 12, 2021 at 10:39 Timothy LeeTimothy Lee 8281 gold badge5 silver badges15 bronze badges 1-
Note that anything consuming
Promise<T>
can't access thec
property anyway - it's not available via that interface, the piler will prevent access to it. – jonrsharpe Commented Oct 12, 2021 at 10:41
1 Answer
Reset to default 5The reason it's not raising an error in your example is that your code isn't immediately returning an object literal, it's returning the result of calling a function. Excess property checks only apply to object literals. Since the function (Promise.resolve
) is returning Promise<{a: boolean, b: boolean, c: number}>
and that's assignment-patible with ok
's Promise<{a: boolean, b: boolean}>
return type, TypeScript doesn't plain.
You can make TypeScript check the object literal you're passing into Promise.resolve
by providing a type argument to Promise.resolve
:
interface IReturnVal {
a: boolean;
b: boolean;
}
function ok(): Promise<IReturnVal> {
return Promise.resolve<IReturnVal>({
// ^^^^^^^^^^^^
a: true,
b: false,
c: 1, // <== error here as desired
});
}
ok();
Playground link
Alternatively, you can make ok
an async
function:
interface IReturnVal {
a: boolean;
b: boolean;
}
async function ok(): Promise<IReturnVal> {
// ^^^^^
return {
a: true,
b: false,
c: 1, // <== error here as desired
};
}
ok();
Playground link
For the avoidance of doubt: async
functions always return promises. If you return a non-promise, it gets wrapped in a promise as though passed through Promise.resolve
.
本文标签:
版权声明:本文标题:javascript - Typescript "Object literal may only specify known properties" not effects on Promise return funct 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742041912a2417586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论