Skip to content
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

Types with same name but different type parameters #3478

Closed
mariusschulz opened this issue Jun 11, 2015 · 3 comments
Closed

Types with same name but different type parameters #3478

mariusschulz opened this issue Jun 11, 2015 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@mariusschulz
Copy link
Contributor

I was wondering: Are there any plans to support multiple types with the same name, but with a different number of type parameters, like this (similar to the Func<T> type in C#)?

interface Func<TResult> {
    (): TResult;
}

interface Func<T1, TResult> {
    (value: T1): TResult;
}

// ...

The current error message says that "all declarations of an interface must have identical type parameters".

Or, with #1616:

type Func<TResult> = () => TResult;
type Func<T1, TResult> = (value: T1) => TResult;
@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jun 11, 2015
@ahejlsberg
Copy link
Member

No plans to support that. It wouldn't work with classes unless we also introduced name mangling or some scheme for generating multiple constructor functions for the same class name.

If we were going to do anything here I think it would be more along the lines of supporting "type parameter tuples", for example such that you could "spread" a generic tuple type into a function call in a type parametric manner.

type Func<...T, R> = (...T) => R;

function invoke<...T, R>(func: Func<...T, R>, args: [...T]): R {
    return func(...args);
}

function foo(s: string, n: number) {
}
invoke(foo, ["hello", 42]);

But that's just speculation at this point...

@mariusschulz
Copy link
Contributor Author

Interesting. Thank you for the explanation.

Well, to solve the interface naming collision, one could use different names like Func1 (for one argument), Func2 (for two arguments), and so on, although that's not as pretty as in C#. ;-)

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Aug 5, 2015
@RyanCavanaugh
Copy link
Member

If we did do something like this, it would probably be generic type argument defaults rather than overloading based on generic arity.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants