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