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:
- The name of the class can be long
- Name of the class is changeable
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']) {}
}
Search Terms
Self type in classes, Class Scope Types
Suggestion
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']) {}
}
Use Cases
- Easy Self reference and access to class Types
- Easier refactoring of Names
- Scoping of type Variables
Examples
Checklist
My suggestion meets these guidelines:
often when Declaring a class it's very useful to reference the Class itsself as a type internally
For Example
It's nice that I can reference the className inside the class Decloration..
During refactoring I can change the type of
valueAwith a single edit.however:
So we have resorted to making single classes perfile and using this syntax
Search Terms
Self type in classes, Class Scope Types
Suggestion
It would be awesome if
type Selfwas automatically declared as a type inside the class Declaration..(or if you could declare
Selfonly in the scope of the class Declarationie 1. scoping
-ie 2. AutoDeclared
Use Cases
Examples
Checklist
My suggestion meets these guidelines: