-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Closed
Copy link
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
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.
whzx5byb
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug