Reputation:
I have a class that has some pretty complicated types based on the generics passed to the class. I use these same types in a few places and I'm trying to figure out how to make them reusable:
class Foo<T> {
method(): SomeComplicateType<T> {}
method2(): SomeComplicateType<T> {}
}
The only thing I can come up with is this, but it allows the user to overwrite the type which I don't want:
class Foo<T, U=SomeComplicatedType<T>> {
method(): U {}
method2(): U {}
}
Upvotes: 1
Views: 82
Reputation: 23772
Sorry for this obvious suggestion but you could use an alias:
type NiceName<T> = SomeComplicateType<T>
class Foo<T> {
method(): NiceName<T> {}
method2(): NiceName<T> {}
}
Upvotes: 0
Reputation: 276343
TypeScript doesn't allow type creation between the two scopes:
class A<Scope1> {
x():Scope2;
}
What you have found is a way to create it as a chain in Scope1
.
Personally I would bite the bullet and not try to create a new type. I prefer explicit work whenever possible.
Upvotes: 1