Skip to content

Iterating over keys of a union type and adding a union loses the original union typeΒ #46059

@vkarpov15

Description

@vkarpov15

Bug Report

πŸ”Ž Search Terms

union type key iterate

πŸ•— Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export interface PlayerCooldown {
  id: string;
  for: "player";
  playerId: string;
}
export interface TeamCooldown {
  id: string;
  for: "team";
  teamId: string;
}
export type Cooldown = PlayerCooldown | TeamCooldown;

type FilterQuery<T> = {
  // Remove "| number" and the script compiles fine. Error occurs with other types, like "| RegExp"
  [P in keyof T]?: T[P] | number;
};

async function test() {
   let forWho: "team" | "player" = "team";
   await getCooldownFromId(forWho, 'test');
}

async function getCooldownFromId(forWho: "team" | "player", cooldownId: string): Promise <undefined> {
    let _forWho: "player" | "team" = forWho;
    let q: FilterQuery<Cooldown> = { for: _forWho, id: cooldownId };
    return undefined;
}

test();

πŸ™ Actual behavior

Below error:

Type '{ for: "player" | "team"; id: string; }' is not assignable to type 'FilterQuery<Cooldown>'.
  Type '{ for: "player" | "team"; id: string; }' is not assignable to type 'FilterQuery<TeamCooldown>'.
    Types of property 'for' are incompatible.
      Type '"player" | "team"' is not assignable to type 'number | "team" | undefined'.
        Type '"player"' is not assignable to type 'number | "team" | undefined'.

πŸ™‚ Expected behavior

No compiler error. This compiles fine without the | number, adding an extra union type shouldn't break this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions