Skip to content

Member type/type alias inside a class (or also an interface) #57150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
yw662 opened this issue Jan 24, 2024 Β· 4 comments
Closed
6 tasks done

Member type/type alias inside a class (or also an interface) #57150

yw662 opened this issue Jan 24, 2024 Β· 4 comments
Labels
Duplicate An existing issue was already created

Comments

@yw662
Copy link

yw662 commented Jan 24, 2024 β€’

πŸ” Search Terms

member type, type alias, generic

βœ… Viability Checklist

⭐ 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

  1. What do you want to use this for?
    In complex generic classes.
  2. What shortcomings exist with current approaches?
    Have to repeat myself again and again.
  3. What workarounds are you using in the meantime?
    By repeating myself.
@MartinJohns
Copy link
Contributor

Essentially a duplicate of #9889 (and linked issues).

@yw662
Copy link
Author

yw662 commented Jan 24, 2024

Yes it is all about generic inner type, but it said "interface" there. Btw would it be possible in some future versions ?

@yw662
Copy link
Author

yw662 commented Jan 24, 2024

This one is indeeed weaker: I am suggesting the inner type in classes only (and not interface) and accessible from inside only (not outside)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants