1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15/**--- 16 description: > 17 this appendix contains a summary of the grammar found in the main document. 18 typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) 19 and this appendix lists only productions that are new or modified from the ECMAScript grammar. 20 options: 21 target: es2015 22 lib: es2015 23 module: ESNext 24 isCurrent: true 25 ---*/ 26 27 28import { Assert } from '../../../../suite/assert.js' 29 30let num: number = 1024; 31let str: string = "string"; 32let bool: boolean = false; 33let big: bigint = 1n; 34let sym: symbol = Symbol(); 35let obj: object = {}; 36let fun: Function = () => { return "Function"; } 37let udf: undefined = undefined; 38Assert.isNumber(num); 39Assert.isString(str); 40Assert.isBoolean(bool); 41Assert.equal(typeof big, "bigint"); 42Assert.isSymbol(sym); 43Assert.equal(typeof obj, "object"); 44Assert.isFunction(fun); 45Assert.isUndefined(udf); 46 47type Ver = { ver: string }; 48interface Data extends Ver { 49 data: number; 50} 51let v: Data = { data: 1024, ver: "1.0.1" }; 52Assert.equal(v.data, 1024); 53Assert.equal(v.ver, "1.0.1"); 54 55function addNumbers(...num: number[]): number { 56 let n: number = 0; 57 for (let i: number = 0; i < num.length; i++) { 58 n += num[i]; 59 } 60 return n; 61} 62Assert.equal(addNumbers(5, 5, 5, 5, 5), 25); 63 64interface StrIndex { 65 [key: string]: number; 66} 67let si: StrIndex = { C: 3, D: 4 }; 68si['A'] = 1; 69si.B = 2; 70Assert.equal(si.A, 1); 71Assert.equal(si['B'], 2); 72Assert.equal(si.C, 3); 73Assert.equal(si['D'], 4); 74Assert.equal(JSON.stringify(si), '{"C":3,"D":4,"A":1,"B":2}'); 75 76interface NumIndex { 77 [key: number]: string; 78} 79let ni: NumIndex = { 1: 'A' }; 80ni[3] = 'C'; 81Assert.equal(ni[1], 'A'); 82Assert.equal(ni[3], 'C'); 83Assert.equal(JSON.stringify(ni), '{"1":"A","3":"C"}'); 84 85class AB<T, U>{ 86 a: T | undefined; 87 b: U | undefined; 88 public c: string = ""; 89 private d: string = ""; 90 protected e: string = ""; 91 setD(val: string) { 92 this.d = val; 93 } 94 setE(val: string) { 95 this.e = val; 96 } 97 show() { 98 return JSON.stringify({ d: this.d, e: this.e }); 99 } 100 constructor(a?: T, b?: U) { 101 this.a = a; 102 this.b = b; 103 } 104} 105let ab = new AB<number, string>(123, "ABC"); 106Assert.equal(ab.a, 123); 107Assert.equal(ab.b, "ABC"); 108 109let ab2 = new AB<number, number>(1024); 110Assert.equal(ab2.a, 1024); 111Assert.isUndefined(ab2.b); 112 113ab.c = "1024"; 114Assert.equal(ab.c, "1024"); 115 116ab.setD("D"); 117ab.setE("E"); 118Assert.equal(ab.show(), "{\"d\":\"D\",\"e\":\"E\"}"); 119 120type ONS = { n1?: number | string, n2?: number | string }; 121let ons: ONS = { n1: 0, n2: "ABC" }; 122Assert.equal(ons.n1, 0); 123Assert.equal(ons.n2, "ABC"); 124 125 126type NumStrBool = number | string | boolean; 127type NumObjBool = number | object | boolean; 128type NumBool = NumObjBool & NumStrBool; 129let nb: NumBool = 1; 130Assert.isNumber(nb); 131nb = true; 132Assert.isBoolean(nb); 133 134type StrFun = (a: string, b: string) => string; 135let strFun: StrFun = (a: string, b: string) => { return a + b; } 136Assert.equal(strFun("AB", "CD"), "ABCD"); 137 138let a: any; 139let vo: void; 140 141let arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 142Assert.equal(arr[0], 0); 143 144type type0 = "A" | "B" | "C" | "D"; 145let t0: type0 = "A"; 146Assert.equal(t0, "A"); 147 148type NumStrArray = number[] | string[] | (number | string)[]; 149let nsa1: NumStrArray = [1, 3, 5]; 150let ar1 = [1, 3, 5] 151let nsa2: NumStrArray = ["A", "B", "C"]; 152let ar2 = ["A", "B", "C"]; 153let nsa3: NumStrArray = ["A", 1, "B", 2, "C", 3]; 154let ar3 = ["A", 1, "B", 2, "C", 3]; 155Assert.equal(JSON.stringify(nsa1), JSON.stringify(ar1)); 156Assert.equal(JSON.stringify(nsa2), JSON.stringify(ar2)); 157Assert.equal(JSON.stringify(nsa3), JSON.stringify(ar3)); 158 159type ABC = [number, string, boolean]; 160let abc: ABC = [1, "A", true]; 161let abc2 = [1, "A", true]; 162Assert.equal(JSON.stringify(abc), JSON.stringify(abc2));