-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options
Description
π Search Terms
Nullish Coalescing Operator
Type check
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs___
β― Playground Link
π» Code
type Tag = "A" | "B" | "C"
type Something = {
tagsArray?: Tag[]
}
type SomethingElse = {
tagsMap: {[k in Tag]?: true}
}
function map(input : Something[]) : SomethingElse[] {
return input.map((something)=>({
tagsMap: something.tagsArray ?? {}
}));
}
const input : Something[] = [
{tagsArray: ["A", "B", "C"]},
{}
]
console.log(JSON.stringify(map(input)))
π Actual behavior
Typescript happily passed this line
tagsMap: something.tagsArray ?? {}
This allows potentially assigning an array to type {[k in Tag]?: true}
Looks like the Nullish Coalescing Operator confused typescript?
π Expected behavior
Typescript should report an error there
Additional information about the issue
Tested in both 5.9.3 and v6.0.0-dev.20251207
Metadata
Metadata
Assignees
Labels
Not a DefectThis behavior is one of several equally-correct optionsThis behavior is one of several equally-correct options