- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
member type, type alias, generic
β Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
When implementing a generic class with typescript, some types with very long type parameter list may be reused very frequently, eg:
class SomeGenericClass<A, B, C, D, E, F, G> {
    member1: SomeMember<A, B, C, D>
    member2: SomeMember<A, B, C, D>
    constructor(arg: SomeMember<A, B, C, D>) {
        this.member1 = arg
        this.member2 = arg
    }
}It would be nice if those types can be aliased somewhere. This type alias could not be outside the class or the type parameter would be lost:
type Member = SomeMember<A, B, C, D>
class SomeGenericClass<A, B, C, D, E, F, G> {
    member: Member // Wrong: Member is not SomeMember<A, B, C, D>
    ...
    }
}Or it could be:
class SomeGenericClass<A, B, C, D, E, F, G, Member extends SomeMember<A, B, C, D> = SomeMember<A, B, C, D>> {
...
}However it is not good: the type parameter Member is not exactly SomeMember<A, B, C, D>, so you cannot use an instance of SomeMember<A, B, C, D> for Member, esp. if it is overwritten by the user.
It should work nicely if it is declared inside the class:
class SomeGenericClass<A, B, C, D, E, F, G> {
    type Member = SomeMember<A, B, C, D>
    // or: type Member: SomeMember<A, B, C, D>
    member1: Member
    member2: Member
    // or even in the constructor (which may prevent type inferencing, but let's see)
    constructor(arg: Member) {
        this.member1 = arg
        this.member2 = arg
    }
}It may also be used outside the class:
...
type Foo = SomeGenericClass<A, B, C, D, E, F, G>.Member // which equals SomeMember<A, B, C, D>
// or
const someInstance = new SomeGenericClass(arg);
type Foo2 = (typeof someInstance).Memberπ Motivating Example
To simplify and clarify complex generic classes.
π» Use Cases
- What do you want to use this for?
 In complex generic classes.
- What shortcomings exist with current approaches?
 Have to repeat myself again and again.
- What workarounds are you using in the meantime?
 By repeating myself.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created