Satisfies does not work with const assertion
See original GitHub issueBug Report
🔎 Search Terms
satisfies, as const, literal
🕗 Version & Regression Information
- This is a crash
- I was unable to test this on prior versions because
satisfies
is new in 4.9.0 beta
⏯ Playground Link
💻 Code
type Colors = "red" | "green" | "blue";
type RGB = readonly [red: number, green: number, blue: number];
type Palette = Readonly<Record<Colors, string| RGB>>;
const palette1 = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255]
// Expect to pass but throws error
} satisfies Palette as const;
🙁 Actual behavior
Fails with error:
‘const’ assertions can only be applied to references to enum members, or string, number, boolean, array, or object literals.
🙂 Expected behavior
No error because it is a literal and satisfies
should not change the type. Also, other type assertions work fine, so const
should as well.
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:20 (3 by maintainers)
Top Results From Across the Web
Using TypeScript const assertions for fun and profit | Instil
Using TypeScript const assertions to create utility types from immutable data.
Read more >typescript - 'as const' combined with type? - Stack Overflow
They are indeed equal, only problem is as I type, I want to type check that what I typed into const test indeed...
Read more >Typescript's new 'satisfies' operator | by Cefn Hoile - Medium
The satisfies operator has arrived in Typescript 4.9. This article explains the purpose of the new keyword, illustrated by detailed, runnable code examples....
Read more >const assertions are the killer new TypeScript feature
LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of ...
Read more >Assertions | Cypress Documentation
If built-in assertions are not enough, you can write your own assertion function and pass it as a callback to the .should() command....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It wouldn’t—which is the problem here.
as const
applies to object/array literals, so even ifsatisfies T as const
worked now, it wouldn’t do what you want—it would just infer the readonly types to begin with and subsequently fail the check. In order to do what you want the narrowing implied by a const assertion would need to be done as a separate step, which requires an architectural change to the compiler.(in simpler terms
{ … } as const
is one atomic expression potentially with a different type from{ … }
and breaking it apart, e.g. by putting asatisfies
clause in the middle, makes no sense with the current architecture)Yes, it works when reversed as I show in the playground link, but that just creates an unnecessary restriction to make sure everything in the
satisfies
type isreadonly
because the check will fail otherwise.The compiler may be treating it as an expression, but it’s not, and therein lies the bug.