often when Declaring a class it's very useful to reference the Class itsself as a type internally
For Example
class SomeReallyLongClassNameComponentThing {
valueA: SomeReallyLongInportedValueA;
valueB: SomeReallyLongInportedValueb;
doSomethingWithA(val: SomeReallyLongClassNameComponentThing['valueA']) {}
doSomethingWithB(val: SomeReallyLongClassNameComponentThing['valueB']) {}
}
It's nice that I can reference the className inside the class Decloration..
During refactoring I can change the type of valueA with a single edit.
however:
So we have resorted to making single classes perfile and using this syntax
type Self = SomeReallyLongClassNameComponentThing;
class SomeReallyLongClassNameComponentThing {
valueA: SomeReallyLongInportedValueA;
valueB: SomeReallyLongInportedValueb;
doSomethingWithA(val: Self['valueA']) {}
doSomethingWithB(val: Self['valueB']) {}
}
Self type in classes, Class Scope Types
It would be awesome if type Self was automatically declared as a type inside the class Declaration..
(or if you could declare Self only in the scope of the class Declaration
ie 1. scoping
export class SomeReallyLongClassNameComponentThingOne {
type Self = SomeReallyLongClassNameComponentThingOne;
valueA: SomeReallyLongInportedValueA;
valueB: SomeReallyLongInportedValueb;
doSomethingWithA(val: Self['valueA']) {}
doSomethingWithB(val: Self['valueB']) {}
}
export class SomeReallyLongClassNameComponentThingTwo {
type Self = SomeReallyLongClassNameComponentThingTwo;
valueOne: SomeReallyLongInportedValueOne;
valueTwo: SomeReallyLongInportedValueTwo;
doSomethingWithA(val: Self['valueOne']) {}
doSomethingWithB(val: Self['valueTwo']) {}
}
-ie 2. AutoDeclared
export class SomeReallyLongClassNameComponentThingOne {
valueA: SomeReallyLongInportedValueA;
valueB: SomeReallyLongInportedValueb;
doSomethingWithA(val: Self['valueA']) {}
doSomethingWithB(val: Self['valueB']) {}
}
export class SomeReallyLongClassNameComponentThingTwo {
valueOne: SomeReallyLongInportedValueOne;
valueTwo: SomeReallyLongInportedValueTwo;
doSomethingWithA(val: Self['valueOne']) {}
doSomethingWithB(val: Self['valueTwo']) {}
}
My suggestion meets these guidelines:
What about this['valueA'] and this['valueB']? It works and gives the behaviour you're describing:
https://www.typescriptlang.org/play/#code/MYGwhgzhAEDKD2BbApgJWWEICeAZeAdgOYDC4UAcmCiUgA6HIEAuAKgBYCWx0A3gFDRoAN0wBXZAEEA-AC5oEZgCduRANyCR45ACE50AmMQAjZEo2aAJvAQpmXYgHVO9yQApRIefc4QA2gDknhKSAQC6AJR8AL5WNkjIPk4u7DoemN5c-kHaOuFRvLGx-MCEitDcimAEwMjQALwGyADucAnomDj4xGSQEFQ09IwsHKpuERaVzNW1AHTWtokORM6ubgHN8EoA1hABE-xAA
We couldn't make Self have a new meaning (it could already mean something else), so the only plausible solution would be to permit local type aliases in class bodies, which is #7061
Most helpful comment
What about
this['valueA']and this['valueB']? It works and gives the behaviour you're describing:https://www.typescriptlang.org/play/#code/MYGwhgzhAEDKD2BbApgJWWEICeAZeAdgOYDC4UAcmCiUgA6HIEAuAKgBYCWx0A3gFDRoAN0wBXZAEEA-AC5oEZgCduRANyCR45ACE50AmMQAjZEo2aAJvAQpmXYgHVO9yQApRIefc4QA2gDknhKSAQC6AJR8AL5WNkjIPk4u7DoemN5c-kHaOuFRvLGx-MCEitDcimAEwMjQALwGyADucAnomDj4xGSQEFQ09IwsHKpuERaVzNW1AHTWtokORM6ubgHN8EoA1hABE-xAA