TypeScript: Missing type-check for generic types (?)

I’ve run into a mysterious case with a return-type that is clearly wrong - in methods like this one:

interface Foo {
    a: string
    b: number
}

class Hello {
    getFoo(): Promise<Foo> {
        return Promise.resolve("a string")
    }
}

The declared return-type in this example is Promise<Foo>, but the function returns Promise<string>, yet there is no notice/warning/error for this - it compiles without complaint.

In fact, it doesn’t matter what I put in that return type-hint - I can return whatever I want, not just Promises, but any unrelated type, anything goes.

Is this really working as intended? What gives?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Please follow contributing guide and provide a minimal reproducible example. I cannot know what happened without knowing http.post type.