Skip to content

Improve error message for stricter types not matching generic T extends SomeInterface #31739

@patrys

Description

@patrys

Search Terms

generic type is not assignable to type error

Suggestion

It's impossible for a non-generic type to be assignable to a generic that extends an interface. This is understandable as at the time of type checking it's impossible to determine the generic type's fields. It would be nice if the error message explained that instead of just saying that the types are incompatible.

Use Cases

See the example below:

Examples

interface Foo {
    bar: string;
}

function baz<T extends Foo> {
    const fizz: T = { bar: "hello world " };
}

Current error:

Type '{ bar: string; }' is not assignable to type 'T'.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions