Skip to content

Generic parmater with union type not working as expected (not assignable to parameter of type)Β #57299

Closed
@JasonMan34

Description

@JasonMan34

πŸ”Ž Search Terms

union, generic, assignable, not assignable to parameter of type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about union and generics

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBFCGBrApgZSgJwJZgOYwF4YAKAB3g3gFsAuGabPASkID4YBXMAE2QDMcybgG4AUKEiwEKAHIcqAI2QZCJcpVoww8pRhYF2XXgLBCxoqAE9SyGADEuwVVZsg+cJGkw58AHzjWyG4esjrK5hLQMFTICA5gTkQAPAAqMMgAHlDIPBD2jqzEfI50KQA0MOrUdAAKFNSxyhCprADaAAwAuvrsAN6iMDAA9EMwAIIYuPI5sMEutgDkDD4w-tqKygswWHlgILDwEBBYuGDwCgA2tlAglfUx2SpzgTALpgBumwB0A8OjKS8lt48Fsdlp9jBDsdTucrnBbvNXh9Nr9igkyPcmGIAL6iUQjGAAmzbMB8ZQ5YC2ADuIAwiDyh3SGRswGy3BgVIAFjkYMB4BcLisoNyYGjWVhwPjRhMpjEwLN3Ii3mEMKDdhCoSczpdrrcqg9lDBnsSgYxcAtRA94PFgMRpF4zRUAExMURAA

πŸ’» Code

const takeString = (param: string) => undefined;
const takeNumber = (param: number) => undefined;

type Func = typeof takeString | typeof takeNumber;

const metaFunc = <T extends Func>(func: T, param: Parameters<T>[0]) => {
  // Argument of type 'string | number' is not assignable to parameter of type 'never'.
  // Type 'string' is not assignable to type 'never'
  func(param);
}

// Type inference works as expected when calling the function
// Argument of type 'number' is not assignable to parameter of type 'string'
metaFunc(takeString, 2)

πŸ™ Actual behavior

The func invocation is invalid. With the error:

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

πŸ™‚ Expected behavior

TypeScript should recognize that since the same T is used for both func and param, the invocation of func(param) is valid. Similar to how it recognizes that metaFunc(takeString, XXX) should be called with a string rather than a string | number

Additional information about the issue

Potentially related: #51587, #33912, #30581

Note that this is different than https://github.com/microsoft/TypeScript/issues/30581#issuecomment-476417974 since the same T extends Func is used for both func and param, rather than using Func for both types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions