Skip to content

Weird checking errors when using async function, and generic typings #15702

@aherve

Description

@aherve

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"
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions