Closed
Description
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
interface MyResponse<T> {
foo: number,
data: T,
}
const g: () => 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 why
return {}
// data is missing, but still no error
return {
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:
"compilerOptions": {
"experimentalDecorators": true,
"module": "commonjs",
"outDir": "./dist/",
"sourceMap": true,
"strictNullChecks": true,
"target": "es6"
},