Skip to content

Contextual never parameter type gets precedence over the initializer's type #56505

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
Andarist opened this issue Nov 22, 2023 · 2 comments Β· Fixed by #56506
Closed

Contextual never parameter type gets precedence over the initializer's type #56505

Andarist opened this issue Nov 22, 2023 · 2 comments Β· Fixed by #56506
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases

Comments

@Andarist
Copy link
Contributor

πŸ”Ž Search Terms

contextual parameter type initializer never

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231122#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMAeAKHngBUBhPQgDwwBpKaBZEDACx2CLwQzEKgHwASuBwxgpEjCyoA5i3gAKTIwBcNeqiat4AOlOxlRXahAA3EDADaAXQCU8ALwA+eGgDWqHAB3VE82T00GYQxdOkjmNQBbbj4BGK5efiIXXRscLGAAbnJyQhJ1dgBvdiowHDRo+AAGNioAXxb4KqoannBfABksBKwGzQxGRKhGD3gAVkbGty9O9ur4HigiAEEwMGJLDS01ZCJ7XQr4VCgk3QUlZXhWpe8K1ba2FyKgA

πŸ’» Code

declare function test<
  TContext,
  TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>,
>(context: TContext, methods: TMethods): void;

test(
  {
    count: 0,
  },
  {
    checkLimit: (ctx, max = 500) => {},
    hasAccess: (ctx, user: { name: string }) => {},
  },
);

πŸ™ Actual behavior

never gets assigned as the type of max parameter and TS errors on the initializer's type mismatch:

Type 'number' is not assignable to type 'never'.(2322)

πŸ™‚ Expected behavior

I'd expect the type inferred from the initializer to take precedence over never here. After all, we could annotate this contravariant position with max: number = 500 and that wouldn't violate any constraints.

Additional information about the issue

No response

@fatcerberus
Copy link

fatcerberus commented Nov 22, 2023

For what it's worth, this isn't specific to never and in fact is a more general issue with too-narrow parameter types: if the contextual parameter type is, say, 1 | 2 | 3, then this is also an error when it doesn't need to be (a function that takes number is assignable to one that takes 1 | 2 | 3).

@Andarist
Copy link
Contributor Author

Good point. I can probably tweak this somewhat easily by checking the assignability between the initializer's type and the contextual type - just in the right direction.

@andrewbranch andrewbranch added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants