Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Understanding TypeScript Type System: Issue with Generated Arrays and Const Assertions

I believe I’m facing a fundamental understanding issue with TypeScript, as evidenced by the behavior in the following code snippet. I’m trying to understand why a generated array isn’t working and if there’s a way to resolve this.

typescript

const mySlugs = ['post1', 'post2'] as const // working
const mySlugs = (allPosts.map(post => post._raw.sourceFileName)) as const  // not working
export type SlugType = (typeof mySlugs)[number]

The first line (const mySlugs = [‘post1’, ‘post2’] as const;) works as expected, but the second line (const mySlugs = (allPosts.map(post => post._raw.sourceFileName)) as const;) doesn’t (A ‘const’ assertions can only be applied to references to enum members, or string, number, boolean, array, or object literals.ts(1355)).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there something I’m misunderstanding about TypeScript’s type system or const assertions? Any insights or suggestions would be greatly appreciated!

>Solution :

The as const assertion can only be used on simple literals, not expressions in general. From the documentation for the release where it was added:

Caveats

One thing to note is that const assertions can only be applied immediately on simple literal expressions.

That’s what the error message says as well:

A ‘const’ assertions can only be applied to references to enum members, or string, number, boolean, array, or object literals. (1355)

If you want to make your generated array read-only, you can use the ReadOnlyArray utility type:

const mySlugs = allPosts.map(post => post._raw.sourceFileName) as ReadonlyArray<string>;

But that only does one of the three things as const would do on a literal. The result will be an array of string, though, not a tuple containing string literal types.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading