// Welcome to the TypeScript Playground, this is a website
// which gives you a chance to write, share and learn TypeScript.
// You could think of it in three ways:
//
// - A location to learn TypeScript where nothing can break
// - A place to experiment with TypeScript syntax, and share the URLs with others
// - A sandbox to experiment with different compiler features of TypeScript
const anExampleVariable = "Hello World"
const color = ['red','green','blue','yellow'];
console.log(anExampleVariable)
console.log(color)
//BigInt
//const unit = BigInt(1);
//展开数组字面量
const firstHalfYearSeasons = ['Spring', 'Summer'];
const seasons = [...firstHalfYearSeasons, 'Fall', 'Winter'];
console.log(seasons);
//展开对象字面量
const point2d = {
x:0,
y:0,
};
const point3d = {
...point2d,
z:0,
}
console.log(point3d);
//展开函数参数
const nums = [3,1,4];
const max = Math.max(...nums);
console.log(max);
//解构-数组解构
const point = [0,1];
const [x, y] = point;
console.log(x);
console.log(y);
//对象解构
const point2 = {a:0, b:1};
const {a, b} = point2;
console.log("a:${a}, b:${b}");
//短路求值
let x4 = 0;
let a4 = undefined;
console.log(a4?.[++x4]);
console.log(x);
const greeting : string = 'hello, world';
const yes:boolean = true;
const no:boolean = false;
const foo:string = 'foo';
const bar:string = 'bar, ${foo}';
const bin:number = 0b1010;
const oct:number = 0o744;
const integer:number = 10;
const float:number = 3.14;
const hex:number = 0xffffff;
//BigInt literals are not available when targeting lower than ES2020
//const bin_int :bigint = 0b0101n;
const key :symbol = Symbol();
const symbolHasInstance : symbol = Symbol.hasInstance;
const s0:symbol = Symbol();
const s1:symbol = Symbol.for('foo');
const s2:symbol = Symbol.hasInstance;
const s3:symbol = s0;
const s01 : unique symbol = Symbol();
const s11 : unique symbol = Symbol.for('s1');
interface Foo{
// [s0]:string;
[s01]:string;
}
const foo5:undefined = undefined;
const foo6:null = null;
function log(message : string):void{
console.log(message);
}
enum Season{
Spring = 1,
Summer,
Fall,
Winter,
}
const fisrtSeason :Season = Season.Spring;
//const secondSeason :Season = 10;
const secondSeason: Season = 2;
enum Direction{
Up = 'Up',
Down = 'Down',
Left = 'LEFT',
Right = 'Right',
U = Up,
D = Down,
L = Left,
R = Right,
}
const direction:string = Direction.Up;
//const direction2: Direction = 'Up';
//联合成员类型
const up:Direction.Up = Direction.Up;
//联合枚举类型
type UnionDirectionType =
| Direction.Up
| Direction.Down
| Direction.Left
| Direction.Right;
enum Color {
//Black = 0 + 10,居然没有报错
//Black,居然也没有报错
//怀疑是网页版本环境有部分功能缺失
White = 'White',
}
enum Bool {
False = 0,
True = 1,
}
console.log(Bool.False);
console.log(Bool.True);
Bool[Bool.False];
Bool[Bool.True];
enum Bar{
C = 'C',
//编译错误
//B,
}
// To learn more about the language, click above in "Examples" or "What's New".
// Otherwise, get started by removing these comments and the world is your playground.
2024-5-5 TS练习
最新推荐文章于 2024-07-21 18:22:34 发布