-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Description
π Search Terms
- ReadonlyArray assertion
- readonly array type cast
- sufficiently overlaps readonly
π Version & Regression Information
- This is the behavior in every version I tried (tested in 3.5.1, 5.9.3, and nightly)
β― Playground Link
π» Code
interface Item {
readonly id: string;
readonly label: string | null;
}
interface MutableItems {
readonly items: Item[] | null;
}
interface ReadonlyItems {
readonly items: ReadonlyArray<Item> | null;
}
const mockMutableItems = {
items: [
{
id: '1',
},
],
} as MutableItems;
const mockReadonlyItems = {
items: [
{
id: '2',
},
],
} as ReadonlyItems;π Actual behavior
mockReadonlyItems produces error TS2352:
Conversion of type '{ items: { id: string; }[]; }' to type 'ReadonlyItems' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Types of property 'items' are incompatible.
Type '{ id: string; }[]' is not comparable to type 'readonly Item[]'.
Property 'label' is missing in type '{ id: string; }' but required in type 'Item'.
π Expected behavior
Both assertions should behave consistently. Since the mutable Item[] version allows the assertion (correctly recognizing that as is an intentional type override), the ReadonlyArray<Item> version should also allow it.
Additional information about the issue
Other observations:
as const satisfieserrors consistently, which seems good- Omitting
itemsfrommockReadonlyItemsremoves the associated type error, suggesting that the issue is specifically when trying to assert on a definedReadonlyArray
Metadata
Metadata
Assignees
Labels
No labels