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

Why can't I declare a union of functions with different parameter types? #57932

Closed
disog opened this issue Mar 25, 2024 · 3 comments
Closed

Why can't I declare a union of functions with different parameter types? #57932

disog opened this issue Mar 25, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@disog
Copy link

disog commented Mar 25, 2024

πŸ”Ž Search Terms

merge function param union

πŸ•— Version & Regression Information

  • This is reproducible in the playground

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgrgOwMYB4AqA+KBeKAKMAQwCdCBbALijQEocs0BuAWACgAbCYKAMyvmQoAzsGIBLBAHMsAH1iJUCOGQBGEYllx5CVQghB1sWQi1Y88ARhqmgA

πŸ’» Code

type Func<T> = (param: T) => T;
let f: Func<string> | Func<number> = (a: any) => a;
f(1);

πŸ™ Actual behavior

The following error appears in the "Errors" tab:

Argument of type 'number' is not assignable to parameter of type 'never'.

πŸ™‚ Expected behavior

I'd expect the type of the function parameter be a union of the original types, just like it is done for the return type.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 25, 2024
@RyanCavanaugh
Copy link
Member

f might be a Func<string>, in which case it would receive a number.

@disog
Copy link
Author

disog commented Mar 25, 2024

Fair enough. I'll close this issue then. Thanks!

@disog disog closed this as completed Mar 25, 2024
@fatcerberus
Copy link

contravariance strikes again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants