Work as expected:
interface ExampleA {
id: number;
name: `S${string}`;
}
export const exampleA = {
id: 8455,
name: 'Savory'
} as const satisfies ExampleA;
Doesn't work :-(
interface ExampleB {
id: number;
name: `S${string}`;
}
export const exampleB = [
{
id: 8455,
name: 'Savory'
}
] as const satisfies ExampleB[];
Error for exampleB:
Type 'readonly [{ readonly id: 8455; readonly name: "Savory"; }]' does not satisfy the expected type 'ExampleB[]'.
The type 'readonly [{ readonly id: 8455; readonly name: "Savory"; }]' is 'readonly' and cannot be assigned to the mutable type 'ExampleB[]'.ts(1360)
I read TypeScript 4.9 blog post and couple of GitHub issue from TypeScript repo and still have no idea what I'm doing wrong or if there another way to do what I'm trying to do.
CodePudding user response:
When you use as const
on an array, typescript thinks of it as readonly
. So, you need to make your satisfies
type readonly
.
export const exampleB = [{...}] as const satisfies readonly ExampleB[];