Skip to content

InstanceType<(typeof this)["somefield"]> type issue #51013

Closed
@imsamurai

Description

@imsamurai

Hi, for inheritance purposes i need to type methods depending on class returned by getter, but i get 'is not assignable' error.
Is this a bug?

class S1 {}
class S2 extends S1 {}


class A  {
   get x() {
    return S1
  }

  f(): InstanceType<(typeof this)["x"]> {
   return new this.x as InstanceType<(typeof this)["x"]> // no error
  }

  fError(): InstanceType<(typeof this)["x"]> {
   return new this.x // got error
  }
}


class B extends A {
  get x() {
    return S2
  }

  f1(){
    return this.f();
  }
}


const a = new A;
const s1test = a.f() // S1 type


const b = new B;
const s2test = b.f1() // S2 type

https://www.typescriptlang.org/play?#code/MYGwhgzhAEDKCM0DeBfAUKSNYCZoFMAPAF3wDsATbRVNOzKaAQWmTVegHN9jpCAKAJRJ2HAE48ArmLJx4o9KIBmQgFzQAkmQjEwZYPgAqATwAO+ADzEAFgEsIAbQBEhJwF0AfG3FSZ0MvgA7tA29gB0hNCQmtq6+kZmlqGOLu5eAPTp-gD2BGJi2WIKdKxKAKL5hWoxOnoGJuZWdimunt6sEsTSsgHByRHQmVzZvPiVRazoihjgjABCBCTkVMzt3LwCgu0dvrK4xcrwQiIc0J3dIc1hKoIA3MXTwNmxUdAAvP5BzPdPLxDwpB07yi1yEgyyCBCiToGGeQIARsDetA5j84bwIDhAbwPvDrkctkNcFDzEA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions