You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It feels to me like this could be a compiler issue:
I'm trying to get the compiler to check the type of a promise, but I get a strange behavior. The following code show 4 different return options I tried:
TypeScript Version: 2.3.2
Code
interfaceMyResponse<T>{foo: number,data: T,}constg: ()=>Promise<MyResponse<number>>=async()=>{// This looks perfectly fine to me:// ERROR: Type 'string' is not assignable to type 'number'.return{foo: 1,data: 'wrong type'}// Both foo and data are missing, but I get no error. I don't get whyreturn{}// data is missing, but still no errorreturn{foo: 1}// Now the compiler complains about `data` being missing// ERROR: Property 'data' is missing in type '{ foo: number; bar: string; }'.return{foo: 1,bar: 'this fails'}}
Expected behavior:
I would expect the compiler to warn me about missing properties if I return an empty or partially filled object
Actual behavior:
As described in the code comments
If this is of any relevance, here is my compiler options:
This is another manifestation of #11022, should be fixed by #15104.
All these return statements should be flagged as errors in typescript@next.
c:\ts>type a.ts
interface MyResponse<T> {
foo: number,
data: T,
}
letg: () => Promise<MyResponse<number>>;
g = async () => {
return {
foo: 1,
data: 'wrong type'
}
};
g = async () => {
return {}
};
g = async () => {
return {
foo: 1
}
};
g = async () => {
return {
foo: 1,
bar: 'this fails'
}
};
c:\ts>tsc --v
Version 2.4.0-dev.20170509
c:\ts>tsc --t es6 --pretty a.ts
8 g = async () => {
~
a.ts(8,1): error TS2322: Type '() => Promise<{ foo: number; data: string; }>' is not assignable to type'() => Promise<MyResponse<number>>'.
Type 'Promise<{ foo: number; data: string; }>' is not assignable to type'Promise<MyResponse<number>>'.
Type '{ foo: number; data: string; }' is not assignable to type'MyResponse<number>'.
Types of property 'data' are incompatible.
Type 'string' is not assignable to type'number'.
15 g = async () => {
~
a.ts(15,1): error TS2322: Type '() => Promise<{}>' is not assignable to type'() => Promise<MyResponse<number>>'.
Type 'Promise<{}>' is not assignable to type'Promise<MyResponse<number>>'.
Type '{}' is not assignable to type'MyResponse<number>'.
Property 'foo' is missing intype'{}'.
19 g = async () => {
~
a.ts(19,1): error TS2322: Type '() => Promise<{ foo: number; }>' is not assignable to type'() => Promise<MyResponse<number>>'.
Type 'Promise<{ foo: number; }>' is not assignable to type'Promise<MyResponse<number>>'.
Type '{ foo: number; }' is not assignable to type'MyResponse<number>'.
Property 'data' is missing intype'{ foo: number; }'.
25 g = async () => {
~
a.ts(25,1): error TS2322: Type '() => Promise<{ foo: number; bar: string; }>' is not assignable to type'() => Promise<MyResponse<number>>'.
Type 'Promise<{ foo: number; bar: string; }>' is not assignable to type'Promise<MyResponse<number>>'.
Type '{ foo: number; bar: string; }' is not assignable to type'MyResponse<number>'.
Property 'data' is missing intype'{ foo: number; bar: string; }'.
It feels to me like this could be a compiler issue:
I'm trying to get the compiler to check the type of a promise, but I get a strange behavior. The following code show 4 different
return
options I tried:TypeScript Version: 2.3.2
Code
Expected behavior:
Actual behavior:
As described in the code comments
If this is of any relevance, here is my compiler options:
The text was updated successfully, but these errors were encountered: