Reputation: 395
type myType = 'A';
const myVar = 'A';
let a: 'A'; // OK
let b: myType; // OK
let c: myVar; // ERROR : cannot find name 'myVar'
Why isn't it possible to use a var as a type ? Shouldn't it be possible ?
Upvotes: 0
Views: 78
Reputation: 7059
There are 4 ways to delcare a variable in TypeScript
var, let, const & Type (Type will be custom datatype)
let & var
are quite similar while const
prevent re-assignment to a variable. You can find diff between var & let here.
const
is something which is constant and it's behaviour in TypeScript is same as C#. In C#, we also can't use const as type.
And to mainly answer your question - myVar is a variable not a type so you can't use a variable as type.
Upvotes: 2
Reputation: 249666
myVar
is a constant, you cannot type a variable as a constant, you can specify that a variable has the same type as the constant:
let c: typeof myVar;
Note that since the constant is "A" the above declaration is the same as
let c: "A";
So the only value assignable tp c
is the constant value "A"
Upvotes: 2