Skip to content

Declaration order dependence when inferring to a union #45603

Closed
@afonsof

Description

@afonsof

Bug Report

🔎 Search Terms

complex types generic inference

🕗 Version & Regression Information

TS Version: ^4.2

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about incorrect types function parameters

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Action<TName extends string,TPayload> {
    name: TName,
    payload: TPayload
}

const actionA = { payload: 'any-string' } as Action<'ACTION_A', string>
const actionB = { payload: true } as Action<'ACTION_B', boolean>

function call<TName extends string,TPayload>(
  action: Action<TName,TPayload>,
  fn: (action: Action<TName,TPayload>)=> any,
) {
  fn(action)
}

const printFn = (action: typeof actionA | typeof actionB)=> console.log(action)

// It works
call(actionA, printFn)
// It crashes
call(actionB, printFn)

//workaround
call<'ACTION_B', boolean>(actionB, printFn)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions