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

Typescript difference between union types and "indexed tuple" 🤷

Is there any difference between these two types?

type type1 = [String,Number][number]

type type2 = String | Number

const a: type1 = "";
const b: type1 = 1;

const c: type2 = ""
const d: type2 = 1

>Solution :

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

No, there is no difference. If you hover over your type1, you will see that TypeScript has evaluated it and it has resolved into the union String | Number.

You can try it in the TypeScript playground

However, you shouldn’t use String or Number as types in TypeScript. These refer to the String and Number constructors. Rather, you should use string and number.

See this old answer from 2013 for a more detailed explanation of the difference between String and string.

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