Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aherve opened this issue May 9, 2017 · 1 comment
Closed

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

aherve opened this issue May 9, 2017 · 1 comment
Labels
Fixed A PR has been merged for this issue

Comments

@aherve
Copy link

aherve commented May 9, 2017

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"
  },
@mhegazy
Copy link
Contributor

mhegazy commented May 9, 2017

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

let g: () => 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 in type '{}'.


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 in type '{ 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 in type '{ foo: number; bar: string; }'.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 9, 2017
@mhegazy mhegazy added this to the TypeScript 2.4 milestone May 9, 2017
@mhegazy mhegazy closed this as completed May 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants