2024-5-5 TS练习

// 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.
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值