let foo:"hello"
foo = 'hello'
foo = 'a' //不能将a分配给hello类型
实际上单个的字符串字面量类型并不是很有用,但是可以使用联合类型将多个字符串字面量组合成一个类型(形成类似于字符串枚举的作用)。
type CardinalDirection = 'North' | 'East' | 'South' | 'West';
function move(distance: number, direction: CardinalDirection)
{
}
move(1,'North')
enum States{
North = 'north',
East = 'east',
South = 'south',
West = 'west'
}
function move1(distance: number, direction: States)
{
}
move1(1,States.North)