Friday, May 3, 2024
HomeJavaScriptThe way to sort mounted string worth mixtures with template literal sorts

The way to sort mounted string worth mixtures with template literal sorts


Printed at

Up to date at

Studying time
1min

Typically, I am shocked about how good TypeScript is. Let’s assume you wish to sort your app’s launch model numbers. The format is [releaseMonth]-[releaseYear]. There will not be a brand new model each month or yr.

How onerous is it to outline a kind for this? It is not onerous in any respect.

js

// outline launch years

sort releaseYear = '2022' | '2023' | '2024';

// outline launch months

sort releasteMonth = '03' | '07' | '11';

// mix the 2 sorts in a template literal sort

sort versionNumber = `$ "07" .$ "2023" `;

 

const model: versionNumber = '04.2022';

Sort '"04.2022"' is just not assignable to sort '"03.2022" | "03.2023" | "03.2024" | "07.2022" | "07.2023" | "07.2024" | "11.2022" | "11.2023" | "11.2024"'. Did you imply '"03.2022"'?2820Sort '"04.2022"' is just not assignable to sort '"03.2022" | "03.2023" | "03.2024" | "07.2022" | "07.2023" | "07.2024" | "11.2022" | "11.2023" | "11.2024"'. Did you imply '"03.2022"'?

For these conditions, TypeScript defines Template Literal Varieties based mostly on good previous template strings. Candy!

Was this snippet useful?
Sure? Cool! You may wish to take a look at Net Weekly for extra snippets. The final version went out 9 days in the past.

Associated Matters

Associated Articles

Concerning the creator

Stefan standing in the park in front of a green background
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments