question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Satisfies does not work with const assertion

See original GitHub issue

Bug 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

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:open
  • Created a year ago
  • Reactions:6
  • Comments:20 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
fatcerberuscommented, Oct 15, 2022

That’s the part I’m stuck on. Why would it ever apply to the satisfies expressions if they are purely checks?

It wouldn’t—which is the problem here. as const applies to object/array literals, so even if satisfies 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 a satisfies clause in the middle, makes no sense with the current architecture)

2reactions
steverepcommented, Oct 14, 2022

Try reversing the order of satisfies and as const. Having the satisfies clause first makes the compiler think you’re trying to apply as const to an expression instead of a bare literal, so it’s rejected for the same reason as (1 + 1) as const would be.

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 is readonly because the check will fail otherwise.

The compiler may be treating it as an expression, but it’s not, and therein lies the bug.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found