Skip to content

Type inference issue in subclasses #15191

Closed
@tommytroylin

Description

@tommytroylin

TypeScript Version: 2.2.2

Code

class A<T> {
  foo: T;
}

class B extends A<'bar'> {
  foo = 'bar';
}

workaround:

class A<T> {
  foo: T;
}

class B extends A<'bar'> {
  foo = 'bar' as 'bar';
}

Expected behavior:
No Error;
Actual behavior:
Type string is not assignable to type 'bar';

I don't know if this behavior is intended.
I think type inference should be more accurate in this case to make programmers not to write as sometype once again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions