diff --git a/es2panda/test/ts_extra_tests/package.json b/es2panda/test/ts_extra_tests/package.json new file mode 100644 index 0000000000000000000000000000000000000000..33830f776ab202538291496d856b9ba5fd8c7ce6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "devDependencies": { + "@types/node": "^18.15.11" + } +} diff --git a/es2panda/test/ts_extra_tests/run_ts_case.py b/es2panda/test/ts_extra_tests/run_ts_case.py new file mode 100644 index 0000000000000000000000000000000000000000..0e17baae8598404266f9c1729cbc2892f886427c --- /dev/null +++ b/es2panda/test/ts_extra_tests/run_ts_case.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import argparse +import os +import shutil + +from tool.test_helper import get_path_file, get_disable_list, is_disable_case +from tool.testcfg import TestCase + +TEST_PATH = './' +TEST_TMP_PATH = './testTmp4/' +TEMP_PATH = os.getcwd() + TEST_TMP_PATH + +if os.path.exists(TEMP_PATH): + shutil.rmtree(TEMP_PATH) + +if not os.path.exists(TEMP_PATH): + os.mkdir(TEMP_PATH) + +total_case = 0 +failed_case = 0 +TestCase.temp_path = TEMP_PATH + + +def is_testcase_exist(parsers, arg): + if not os.path.isabs(arg): + arg = TEST_PATH + arg + if not os.path.exists(arg): + parsers.error("The directory or file '%s' does not exist" % arg) + return os.path.abspath(arg) + + +def is_file(parsers, arg): + if not os.path.isfile(arg): + parsers.error("The file '%s' does not exist" % arg) + + return os.path.abspath(arg) + + +def is_directory(parsers, arg): + if not os.path.isdir(arg): + parsers.error("The directory '%s' does not exist" % arg) + + return os.path.abspath(arg) + + +def parse_and_execute(path, ark_runtime=False, skip_negative=True): + if path.endswith(".ts"): + test_cases = TestCase(path) + if not test_cases.is_test_case: + return False, False + # check test case declare + if not test_cases.check_declaration(): + print(test_cases.path, test_cases.detail_result, sep='\t') + return True, True + if skip_negative and test_cases.is_negative(): + return False, False + else: + test_cases.execute(ark_runtime) + if test_cases.fail: + print('TESTCASE Fail! Fail reason is coming:') + print(test_cases.path, test_cases.detail_result, sep='\t') + return True, True + return True, False + else: + return False, False + + +# create a parser object +parser = argparse.ArgumentParser(description="TypeScript Spec&Feature Test Tool") + +# files or command +parser.add_argument("release", nargs='*', metavar="release", type=lambda arg: is_testcase_exist(parser, arg), + help="All test case in the release will be execute") + +parser.add_argument("-a", "--ark_runtime", action="store_true", default=False, help="test on ark_runtime") + +parser.add_argument("-s", "--skip-abnormal-case", action="store_true", default=False, help="skip abnormal test case") + +parser.add_argument("-v", "--version", dest='limit_version', default=None, help="version limit") + +# skip list +parser.add_argument("-d", "--disable-list", type=lambda arg: is_file(parser, arg), default=None, + help="path to the file that contains test to skip") + +parser.add_argument( + '--js-runtime', dest='js_runtime_path', default=None, type=lambda arg: is_directory(parser, arg), + help='the path of js vm runtime') +parser.add_argument( + '--LD_LIBRARY_PATH', dest='ld_library_path', default=None, help='LD_LIBRARY_PATH') + +parser.add_argument( + '--es2abc', dest='es2abc', default=None, help='ES2ABC_PATH') + +parser.add_argument( + '-tsc', dest='tsc', default="tsc", help='tsc') + +# parse the arguments from standard input +args = parser.parse_args() +if args.js_runtime_path: + TestCase.js_runtime_path = args.js_runtime_path +if args.ld_library_path: + TestCase.ld_library_path = args.ld_library_path +if args.es2abc: + TestCase.es2abc = args.es2abc + +TestCase.tsc = args.tsc + +disable_list = [] +if args.disable_list: + disable_list = get_disable_list(args.disable_list) + +# tsc + node / es2abc +print("TEST CASE", "FAIL REASON", "FAIL LINE", sep="\t") + +for file_path in args.release: + root = file_path + # gen abc file + if args.ark_runtime: + for file_paths in get_path_file("suite", None, True): + if file_paths.endswith(".ts"): + test_case = TestCase(file_paths) + if not test_case.is_test_case: + test_case.create_abc(file_paths) + + for file_paths in get_path_file("test_ts_cases", None, True): + if file_paths.endswith(".ts"): + test_case = TestCase(file_paths) + if not test_case.is_test_case: + test_case.create_abc(file_paths) + + if is_disable_case(file_path, disable_list): + continue + if os.path.isfile(file_path): + is_test_count, failed = parse_and_execute(file_path, args.ark_runtime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed: + failed_case += 1 + continue + for file_paths in get_path_file(file_path, None, True, args.limit_version): + if not file_paths.endswith(".ts"): + continue + if is_disable_case(file_paths, disable_list): + continue + is_test_count, failed = parse_and_execute(file_paths, args.ark_runtime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed: + failed_case += 1 + +# delete abc files +if args.ark_runtime: + for file_paths in get_path_file("suite", None, True): + if file_paths.endswith(".abc"): + if os.path.exists(file_paths): + os.remove(file_paths) + + for file_paths in get_path_file("test_ts_cases", None, True): + if file_paths.endswith(".abc"): + if os.path.exists(file_paths): + os.remove(file_paths) + if file_paths.endswith(".ts"): + if os.path.exists(file_paths): + file = open(file_paths, 'r') + lines = file.readlines() + if lines[-1] == 'print("TESTCASE SUCCESS");': + lines.pop() + lines[-1] = lines[-1].replace('\n', '') + file = open(file_paths, 'w') + file.writelines(lines) + file.close() + +print("TOTAL CASE COUNT:%d" % total_case) +print("SUCCESS CASE COUNT:%d" % (total_case-failed_case)) +print("FAILED CASE COUNT:%d" % failed_case) +# delete temp dir +if os.path.exists(TEMP_PATH): + shutil.rmtree(TEMP_PATH) diff --git a/es2panda/test/ts_extra_tests/suite/assert.ts b/es2panda/test/ts_extra_tests/suite/assert.ts new file mode 100644 index 0000000000000000000000000000000000000000..503047c090cf6ef94a66bca8c28f232b495b5985 --- /dev/null +++ b/es2panda/test/ts_extra_tests/suite/assert.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import { AssertionError } from "./assertionError.js" + +export class Assert { + private static defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + + } + static equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect)); + } + } + static notEqual(actual: any, expect: any, msg?: string) { + if (actual == expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect, false)); + } + } + static isTrue(actual: any, msg?: string) { + this.equal(actual, true, msg); + } + static isFalse(flag: any, msg?: string) { + this.equal(flag, false, msg); + } + static isNumber(x: any, msg?: string) { + this.equal(typeof x, "number", msg); + } + static isString(s: any, msg?: string) { + this.equal(typeof s, "string", msg); + } + static isBoolean(s: any, msg?: string) { + this.equal(typeof s, "boolean", msg); + } + static isSymbol(actual: any, msg?: string) { + this.equal(typeof actual, "symbol", msg); + } + static isFunction(fun: any, msg?: string) { + this.equal(typeof fun, "function", msg); + } + static notNULL(v: any, msg?: string) { + this.notEqual(v, null, msg); + } + static isUndefined(actual: any, msg?: string) { + this.equal(actual, undefined, msg); + } + static isObject(obj: any, msg?: string) { + this.equal(typeof obj, "object", msg); + } +} \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/suite/assertionError.ts b/es2panda/test/ts_extra_tests/suite/assertionError.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1447b867de92c15664ca6c409dc97949b0f6f18 --- /dev/null +++ b/es2panda/test/ts_extra_tests/suite/assertionError.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export class AssertionError extends Error { + protected msg: string | undefined = ""; + + constructor(msg: string | undefined) { + super(); + this.msg = msg; + } +} diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/Intersection_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/Intersection_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..14ce4b4a98ee4a4973f1996bb8aadbb141755bf6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/Intersection_types_1.ts @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Intersection types represent values that simultaneously have multiple types. + A value of an intersection type A & B is a value that is both of type A and type B. + Intersection types are written using intersection type literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface A1 { + num1: number; +} +interface B1 { + str1: string; +} +let x: A1 & B1 = { num1: 1, str1: "b" }; +Assert.equal(x.num1, 1); +Assert.equal(x.str1, "b"); +enum Color { + Red1 = 1, + Green1, + Blue1, +} +interface A2 { + num2: [string, number]; +} +interface B2 { + str2: Color; +} +let x2: A2 & B2 = { num2: ["a2", 1], str2: Color.Red1 }; +Assert.equal(x2.num2[0], "a2"); +Assert.equal(x2.str2, 1); +interface A3 { + num3: number[]; +} +interface B3 { + str3: boolean; +} +let x3: A3 & B3 = { num3: [1, 2, 3], str3: true }; +Assert.equal(x3.str3, true); +interface A4 { + num4: number; +} +interface B4 { + str4: string; +} +interface C4 { + cm: any; +} +let x4: A4 & B4 & C4 = { num4: 1, str4: "b4", cm: 3 }; +Assert.equal(x4.num4, 1); +Assert.equal(x4.str4, "b4"); +Assert.equal(x4.cm, 3); +interface XX { + obj: A1; +} +interface YY { + obj: B1; +} +let x5: XX & YY = { obj: x }; +Assert.equal(x5.obj.num1, 1); +interface A5 { + x: number; +} +interface B5 { + x: number; +} +let x6: A5 & B5 = { x: 1 }; +Assert.equal(x6.x, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/intersection_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/intersection_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..27fc57353da439b3bb6135c2eb6364c7e9ca063f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/Intersection_Types/intersection_types_2.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The union and intersection type operators can be applied to type parameters. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function getSmallPet(name: string | number) { + return name; +} +let pet = getSmallPet("fishbird"); +Assert.equal(pet, "fishbird"); +interface Person1 { name: string } +interface People1 { sex: string } +type PersonMan = Person1 & People1 +function getPerson(person: PersonMan) { + return person.name; +} +let man: PersonMan = { + name: "join", + sex: "man" +} +let getpersonname = getPerson(man); +Assert.equal(getpersonname, "join"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d8e97e6992d4e2bfc23e41aca3d0160fe006cb96 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_1.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Classes, enums, and type aliases are named types that are introduced through class declarations, + interface declarations, enum declarations, and type alias declarations. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Test { + name: string; + constructor(name: string) { + this.name = name; + } +} +let tt = new Test("caihua"); +Assert.equal(tt.name, "caihua"); +interface A { + a: string; +} +function f(obj: A) { + obj.a = obj.a + "bb"; +} +let obj: A = { a: "aa" }; +f(obj); +Assert.equal(obj.a, "aabb"); +enum Color { + Red, + Green, + Blue, +} +let a: Color.Red = Color.Red; +Assert.equal(a, Color.Red); +type pp = number | string; +let b: pp; +b = 10; +Assert.equal(b, 10); +b = "hello"; +Assert.equal(b, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4da21efaaaf72028f9b08959c6a7229f1e6ffddd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + class declarations with only public members introduce named types + that function exactly like those created by interface declarations. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface TestInterface { + age: number; +} +class TestClass { + public age: number; + constructor(age: number) { + this.age = age + } +} +function test1(v: TestInterface) { + return v.age; +} +function test2(v: TestClass) { + return v.age; +} +let ee = { + age: 18, +}; +Assert.equal(test1(ee), 18); +Assert.equal(test2(ee), 18); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b50a04cf1ee71b98cf5142dc2d0d2944cb1b4ef2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_3.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Generic types are "templates" from which multiple actual types can be created by writing type references + that supply type arguments to substitute in place of the generic type's type parameters + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Person { + name: string; + age: number; + getName(name: string): string; +} +type Optional = { [P in keyof T]: T[P] }; +let cc: Optional = { + age: 18, + name: "caihua", + getName(name: string) { + return name; + }, +}; +Assert.equal(cc.age, 18); +Assert.equal(cc.name, "caihua"); +Assert.equal(cc.getName("caihua"), "caihua"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..7fe8901575946ef6254a95cc44d14dc7249f272f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/named_types/named_types_4.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript has a structural type system, and therefore an instantiation of a generic type + is indistinguishable from an equivalent manually written expansion. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} +interface I { + front: T; + later: U; +} +function test(v: I) { + Assert.equal(v.front, "abc"); + Assert.equal(v.later.x, 1); + Assert.equal(v.later.y, 1); +} +test({ front: "abc", later: { x: 1, y: 1 } }); +let cc: I; +cc = { + front: "abc", + later: { + x: 1, + y: 1, + }, +}; +test(cc); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b950b0e7b3ddaa43938de7385a9d8e91e920ad9c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_1.ts @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + array types represent JavaScript arrays with a common element type. + array types are named type references created from the generic interface type 'Array' in the global namespace with the array element type as a type argument. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr1: Array = ["ABC", "DEF", "GHI"]; +let arr2: Array = [1, 3, 5, 7, 9]; +let arr3: Array = [true, false]; +let arr4: Array = [Object, { 0xff: "0xFF" }]; +Assert.equal(arr1[0], "ABC"); +Assert.equal(arr2[0], 1); +Assert.equal(arr3[0], true); +Assert.equal(arr4[0], Object); +let objArray: object[] = []; +arr1.forEach(function (element, index, arr) { + objArray[index] = { index: index, element: element, arr: arr }; +}); +Assert.equal(arr2.toString(), "1,3,5,7,9"); +Assert.equal(arr3.length, 2); +Assert.equal(arr2.pop(), 9); +Assert.equal(arr2.toString(), "1,3,5,7"); +arr3[0] = false; +Assert.equal(arr3[0], false); +interface ArrayNumber { + [x: number]: number; +} +let arr5: ArrayNumber = arr2; +Assert.equal(arr5[0], 1); +Assert.equal(arr5[1], 3); +class ArrayString { + [x: number]: string; +} +let arr6: ArrayString = arr1; +Assert.equal(arr6[0], "ABC"); +Assert.equal(arr6[1], "DEF"); +type ArrayType = { + [x: number]: number; +} +let arr7: ArrayType = [1, 2, 3]; +Assert.equal(arr7[0], 1); +Assert.equal(arr7[1], 2); +let arr8: Object[]; +arr8 = [ + 111, + true, + "aaa" +]; +Assert.equal(arr8[0], 111); +Assert.equal(arr8[1], true); +Assert.equal(arr8[2], "aaa"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c8c61db45851330e0958f23e2eef3095e2e9795 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_2.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the declaration of the 'Array' interface includes a property 'length' and a numeric index signature for the element type, along with other members + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: Array = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]; +Assert.equal(arr[0], "A"); +Assert.equal(arr[5], "F"); +Assert.equal(arr.length, 9); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..43a1046c084de4ed762e316a3980068dc3ea99c7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/array_types/array_types_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + array literals may be used to create values of array types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr6: number[] = [1, 3, 5, 7, 9]; +let arr7: boolean[] = [true, false, true, false, true]; +let arr8: string[] = ["a", "b", "c", "d", "e", "f"]; +let arr9: object[] = [{ 0x00: "0x00" }, { 0x01: "0x01" }]; +arr6[3] = 14; +Assert.equal(arr6[3], 14); +Assert.equal(true, arr7[2]); +Assert.equal(arr8.toString(), "a,b,c,d,e,f"); +arr8.pop(); +Assert.equal(arr8.toString(), "a,b,c,d,e"); +arr8.push("0x02"); +Assert.equal(arr8.toString(), "a,b,c,d,e,0x02"); +Assert.isObject(arr9); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e20b34625cdc2b58663149e23c018e17cf3f2d7f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_1.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An object type containing one or more construct signatures is said to be a constructor type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + name: string; + age: number; + constructor(n: string, age: number) { + this.name = n; + this.age = age; + } + run(): string { + return "person"; + } +} +class Teacher extends Person { + run(): string { + return "teacher"; + } +} +type constructor = new (name: string, age: number) => T; +let testClass1: constructor = Teacher; +let testObj1: Person = new testClass1("caihua", 20); +Assert.equal(testObj1.age, 20); +Assert.equal(testObj1.name, "caihua"); +Assert.equal(testObj1.run(), "teacher"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..59218da0105415776b4f0be3ba4c5b68e73db7b5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_2.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Constructor types may be written using constructor type literals + or by including construct signatures in object type literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + name: string; + age: number; + constructor(n: string, age: number) { + this.name = n; + this.age = age; + } + run(): string { + return "person"; + } +} +class Student extends Person { + run(): string { + return "student"; + } +} +class Teacher extends Person { + run(): string { + return "teacher"; + } +} + +let testClass1: new (name: string, age: number) => Person = Student; +let testObj1: Person = new testClass1("caihua1", 12); +Assert.equal(testObj1.age, 12); +Assert.equal(testObj1.name, "caihua1"); +let testClass2: { new(n: string, a: number): Person } = Teacher; +let testObj2: Person = new testClass2("caihua2", 120); +Assert.equal(testObj2.age, 120); +Assert.equal(testObj2.name, "caihua2"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..548816c9996f9b21d773cecef97e8b40c8198101 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_3.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An object type containing one or more construct signatures is said to be a constructor type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I1 { + a: string; + b: boolean; +} +interface I2 { + new(a: string, b: boolean): I1; +} +class MyClass implements I1 { + readonly a: string; + readonly b: boolean; + constructor(a: string, b: boolean) { + this.a = a; + this.b = b; + } +} +function fun( + funConstructor: I2, + a: string, + b: boolean +): I1 { + return new funConstructor(a, b); +} +let x1: I1 = new MyClass("x1", true); +Assert.equal(x1.a, "x1"); +Assert.equal(x1.b, true); +let x2: I1 = fun(MyClass, "x2", true); +Assert.equal(x2.a, "x2"); +Assert.equal(x2.b, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..68ac1268245084eadf0d7aae42b275caca1b4ad6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/constructor_types/constructor_types_4.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An object type containing one or more construct signatures is said to be a constructor type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal { + name: string | undefined; + age: number | undefined; + constructor(); + constructor(name: string); + constructor(age: number); + constructor(name: string, age: number); + constructor(nameorage?: string | number, age?: number) { + if (typeof nameorage == "number") { + this.age = nameorage; + } + if (typeof nameorage == "string") { + this.name = nameorage; + } + if (age) { + this.age = age; + } + } +} +let tt1 = new Animal(); +let tt2 = new Animal("caihua2", 12); +Assert.equal(tt2.name, "caihua2"); +Assert.equal(tt2.age, 12); +let tt3 = new Animal("caihua3"); +Assert.equal(tt3.name, "caihua3"); +let tt4 = new Animal(1230); +Assert.equal(tt4.age, 1230); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c2b2133172dace4c303404aa35db22c87034a5d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an object type containing one or more call signatures is said to be a function type. + function types may be written using function type literals or by including call signatures in object type literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let fun1: (num1: number, num2: number) => number = ( + num1: number, + num2: number +) => { + return num1 + num2; +}; +Assert.equal(fun1(3, 5), 8); +let fun2: { (num1: number, num2: number, num3: number): number } = ( + num1: number, + num2: number, + num3: number +) => { + return num1 + num2 + num3; +}; +Assert.equal(fun2(1, 3, 5), 9); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..30c20b90cea794f3db6d9e931ec325effa29f452 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/function_types/function_types_2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an object type containing one or more call signatures is said to be a function type. + function types may be written using function type literals or by including call signatures in object type literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function add(x: number, y: number): number { + return x + y; +} +let a: number = add(1, 2); +Assert.equal(a, 3); +let add2 = function (x: number, y: number): number { return x + y; }; +let b: number = add2(3, 4); +Assert.equal(b, 7); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ef706cd201e3c51f65e05cb7187d995811709b9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_1.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Properties in a class declaration may be designated public, private, or protected, + while properties declared in other contexts are always considered public. + Private members are only accessible within their declaring class. + Protected members are only accessible within their declaring class and classes derived from it. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class C { + public h_pub: string; + private h_pri: string; + protected h_pro: string; + constructor(h_pub: string, h_pri: string, h_pro: string) { + this.h_pub = h_pub; + this.h_pri = h_pri; + this.h_pro = h_pro; + } + set(h_pri: string) { + this.h_pri = h_pri; + } + get() { + return this.h_pri; + } + output() { + return this.h_pro; + } +} +let x = new C('Public', 'Private', 'Protected'); +Assert.equal(x.h_pub, 'Public'); +Assert.equal(x.get(), 'Private'); +Assert.equal(x.output(), 'Protected'); +class Child extends C { } +let y = new Child('public', 'private', 'protected'); +Assert.equal(y.output(), 'protected'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d29630a0b126ae28a60299b04f35d10f90ad5a7e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Properties in an object type literal or interface declaration may be designated required or optional, + while properties declared in other contexts are always considered required. + Properties that are optional in the target type of an assignment may be omitted from source objects. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I { + h_name: string; + h_age: number; + h_height?: number; + h_weight?: number; +} +let h_s: I = { + h_name: 'xiao', + h_age: 19, + h_height: 180 +} +Assert.equal(h_s.h_name, 'xiao'); +Assert.equal(h_s.h_age, 19); +Assert.equal(h_s.h_height, 180); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..521985e8d3fd1c220226b3054c16696136281e0b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/members/members_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Index signatures, which define type constraints for properties in the given type. + An object type can have at most one string index signature and one numeric index signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface numberIndex { + [index: number]: string +} +let numberTest: numberIndex = ['1', '2', '3'] +Assert.equal(numberTest[0], '1'); +Assert.equal(numberTest[1], '2'); +Assert.equal(numberTest[2], '3'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/named_type_reference/named_type_reference.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/named_type_reference/named_type_reference.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf6ef7908326fa31b800b0aa8526fffc4029acb7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/named_type_reference/named_type_reference.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type references to class and interface types are classified as object types. + Type references to generic class and interface types include type arguments that are substituted for the type parameters of the class + or interface to produce an actual object type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class C1 { + num1: number; + num2: number; + constructor(num1: number, num2: number) { + this.num1 = num1; + this.num2 = num2; + } + add(x: number, y: number): void { + let sum: number = x + y; + Assert.equal(sum, 10); + } +} +let o: C1 = new C1(4, 6); +o.add(3, 7); + +interface I { + name: string; + age: number; + greet: () => string +} +let i: I = { + name: 'xiao', + age: 18, + greet() { return "hello"; } +} +Assert.equal(i.name, "xiao"); +Assert.equal(i.age, 18); +Assert.equal(i.greet(), "hello"); +class C2 { + x: T; + constructor(x: T) { + this.x = x; + } + getx() { + return this.x; + }; +} +let c2 = new C2(1); +Assert.equal(c2.getx(), 1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..adf7b4858e4b493125cc616d39b2601d874a74a2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Tuple types represent JavaScript arrays with individually tracked element types. + A tuple type combines a set of numerically named properties with the members of an array type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let cc: [number, string, boolean]; +cc = [12, "abcd", true]; +Assert.equal(cc[0], "12"); +Assert.equal(cc[0].toString(), "12"); +Assert.equal(cc[1], "abcd"); +Assert.equal(cc[1].length, 4); +Assert.equal(cc[2], true); +let dd = cc[2] ? 0 : 1; +Assert.equal(dd, 0); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ecbffe23bcd9e40f0a22f64254b1732737678aa2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Array literals may be used to create values of tuple types. + the members of an array type whose element type is the union type of the tuple element types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let cc: [number, string, boolean]; +cc = [12, "abcd", true]; +let index: number = 0; +let x = cc[index]; +x = 12; +Assert.equal(x, 12); +x = false; +Assert.equal(x, false); +x = "string"; +Assert.equal(x, "string"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..333748d2aad007d84c48486584ff44eef3651c3e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/object_types/tuple_types/tuple_types_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Named tuple types can be created by declaring interfaces that derive from Array + and introduce numerically named properties + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface tt extends Array { + 0: K; + 1: V; +} +let x: tt = [10, "ten"]; +Assert.equal(x[0], 10); +Assert.equal(x[1], "ten"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7250fab342f80575e10ef8f555c4955d380c6bb4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Boolean primitive type corresponds to the similarly + named JavaScript primitive type and represents logical values that are either true or false. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function test1(): number { + return 0; +} +function test2(): number { + return 1; +} +function test3(): number { + return 1; +} +function test4(i: number): number { + return i; +} +let a: boolean = true; +Assert.equal(a, true); +let b: boolean = false; +Assert.equal(b, false); +let c: number = a ? 0 : 1; +Assert.equal(c, 0); +c = b ? 0 : 1; +Assert.equal(c, 1); +if (a) { + Assert.equal(test1(), 0); +} else { + Assert.equal(test2(), 1); +} +while (a) { + Assert.equal(test3(), 1); + break; +} +for (let i = 0; a && i < 5; i++) { + Assert.equal(test4(i), i); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..bac6e838d658b603e977ed10839976b7420219b1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The boolean keyword references the Boolean primitive + type and the true and false literals reference the two Boolean truth values. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let a: boolean = true; +Assert.isBoolean(a); +Assert.equal(a, true); +a = false; +Assert.isBoolean(a); +Assert.equal(a, false); + +let b: boolean = 2 > 1; +Assert.isBoolean(b); +Assert.equal(b, true); +b = !b; +Assert.isBoolean(b); +Assert.equal(b, false); + +let c: boolean = 2 > 1 && 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, true); +c = 2 < 1 && 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, false); + +c = 2 > 1 || 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, true); +c = 2 < 1 || 7 > 8; +Assert.isBoolean(c); +Assert.equal(c, false); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4f7d5a5ef68c7fd195355a1b2a3f950e3c8327e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Boolean primitive type behaves as an object + type with the same properties as the global interface type 'Boolean'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let a = true; +Assert.equal(a.valueOf(), true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..bdac5a3c602dd6111eae2d81afba0f7750ddfe15 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Enum types are distinct user defined subtypes of the Number primitive type and vice versa + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +enum Color { + Red, + Green, + Blue, + Black, +} +let cc: Color = Color.Blue; +Assert.equal(cc, 2); +let ee: Color = Color.Blue; +let dd: number = cc; +Assert.equal(dd, Color.Blue); +dd = 15; +ee = dd; +Assert.equal(ee, 15); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_null_type/the_null_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_null_type/the_null_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..11de69e57b6d48b4e3a159a15cc37d7a9c034374 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_null_type/the_null_type_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The Null type corresponds to the similarly named JavaScript primitive type and is the type of the null literal. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let n: null = null; +let flag = false; +if (n == null) { + flag = true; +} +Assert.equal(flag, true); +let x1: any = null; +Assert.equal(x1, null); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7581d7f67e6ac81c444858089b5c3eaba6071139 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_1.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Test "Number primitive type corresponds to the similarly named JavaScript primitive type + and represents double-precision 64-bit format IEEE 754 floating point values." + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x1: number; +let x2: number; +let x3: number; +x1 = 0.1; +x2 = 0.2; +x3 = 0.3; +Assert.notEqual(x1 + x2, 0.3); +x2 = 0.7; +Assert.notEqual(x1 + x2, 0.8); +x1 = 0.2; +x2 = 0.4; +Assert.notEqual(x1 + x2, 0.6); +x1 = 1.5; +x2 = 1.2; +Assert.notEqual(x1 - x2, 0.3); +x1 = 0.3; +x2 = 0.2; +Assert.notEqual(x1 - x2, 0.1); +x1 = 19.9; +x2 = 100; +Assert.notEqual(x1 * x2, 1990); +x1 = 0.8; +x2 = 3; +Assert.notEqual(x1 * x2, 2.4); +x1 = 35.41; +x2 = 100; +Assert.notEqual(x1 * x2, 3541); +x1 = 0.3; +x2 = 0.1; +Assert.notEqual(x1 / x2, 3); +x1 = 0.69; +x2 = 10; +Assert.notEqual(x1 / x2, 0.069); +Assert.equal((1.335).toFixed(2), 1.33); +Assert.equal((1.3335).toFixed(3), 1.333); +Assert.equal((1.33335).toFixed(4), 1.3334); +Assert.equal((1.333335).toFixed(5), 1.33333); +Assert.equal((1.3333335).toFixed(6), 1.333333); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7db2d383562ee3ccc5141da2fc2c5ccceb14d96c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_2.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The number keyword references the Number primitive type and numeric literals may be used to write values of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + + +let n1: number = 1; +Assert.isNumber(n1); +Assert.equal(n1, 1); +n1 = 2; +Assert.isNumber(n1); +Assert.equal(n1, 2); +let n2: number = 1.51; +Assert.isNumber(n2); +Assert.equal(n2, 1.51); +n2 = 3.53; +Assert.isNumber(n2); +Assert.equal(n2, 3.53); +let n3: number = 0b1011; +Assert.isNumber(n3); +Assert.equal(n3, 0b1011); +n3 = 0b1111; +Assert.isNumber(n3); +Assert.equal(n3, 0b1111); +let n4: number = 0o17; +Assert.isNumber(n4); +Assert.equal(n4, 0o17); +n4 = 0o24; +Assert.isNumber(n4); +Assert.equal(n4, 0o24); +let n5: number = 0xf00d; +Assert.isNumber(n5); +Assert.equal(n5, 0xf00d); +n5 = 0xf01c; +Assert.isNumber(n5); +Assert.equal(n5, 0xf01c); +let n6: 1 | 2 = 1; +let n7: number; +n7 = n6; +Assert.equal(n7, 1); +n6 = 2; +n7 = n6; +Assert.equal(n7, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4bfd74f7aeebc6612225c7c54183f89a57a7569 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_number_type/the_number_type_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: the Number primitive type behaves as an object type with the same properties as the global interface type 'Number'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let h_z = 123.456; +let s = h_z.toFixed(2); +Assert.equal(s, 123.46); +let a = h_z.toString(); +Assert.equal(a, "123.456"); +let b = h_z.toExponential(); +Assert.equal(b, "1.23456e+2"); +b = h_z.toExponential(2); +Assert.equal(b, "1.23e+2"); +let c = h_z.toPrecision(); +Assert.equal(c, "123.456"); +c = h_z.toPrecision(3); +Assert.equal(c, "123"); +let d = h_z.valueOf(); +Assert.equal(d, 123.456); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..96d9da95e073c7a073ebcb8b4ab55e3305173674 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + All string literal types are subtypes of the String primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let cc: "hello" = "hello"; +Assert.isString(cc); +let dd: string = cc; +Assert.equal(dd, "hello"); +let ee: "" = ""; +Assert.isString(ee); +let ff: string = ee; +Assert.equal(ff, ""); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f3778850121f3af86ff7bbfcfcdbc2f5e384a90 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Specialized signatures permit string literals to be used as types in parameter type annotations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface Test { + v: "hello"; +} +function test(dd: Test) { + Assert.equal(dd.v.length, 5); + return dd; +} +let cc: Test = { + v: "hello", +}; +Assert.equal(test(cc).v, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..0b823f8aeb07162241e0d71980a8cc223adf1ad3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + String literal types are permitted only in that context and nowhere else. + string literals no longer always have the type string. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let HELLO: "HELLO" = "HELLO"; +let WORLD: "WORLD" = "WORLD"; + +let hello = HELLO.toLowerCase(); +Assert.equal(hello, "hello"); + +let HELLOWORLD = HELLO + WORLD; +Assert.equal(HELLOWORLD, "HELLOWORLD"); + +let a: "foo" | number = "foo"; +Assert.equal(a, 'foo'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d74dc821f0249a0561621d2b29212f8e9bc81b6a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The String primitive type corresponds to the similarly named JavaScript primitive type + and represents sequences of characters stored as Unicode UTF-16 code units. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let str: string = "\u4f60\u597d"; +Assert.equal(str, "你好"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..78d89c6fc242111c89fa3d8a0b0b0e1c9b2f83c7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_2.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The string keyword references the String primitive type and + string literals may be used to write values of the String primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let str: string; +str = "abcd"; +Assert.equal(str, "abcd"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..dd0e5f903e340dbd65818edb55fa5ecae8ba13ce --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_string_type/the_string_type_3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the String primitive type behaves as an object type + with the same properties as the global interface type 'String'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let str: string = "string"; +Assert.equal(str.charAt(1), "t"); +Assert.equal(str.charCodeAt(2), 114); +Assert.equal(str.toString(), 'string'); +Assert.equal(str.concat("new"), "stringnew"); +str = "cbaabcda"; +Assert.equal(str.indexOf("a"), 2); +Assert.equal(str.indexOf("a", 4), 7); +Assert.equal(str.lastIndexOf("b"), 4); +Assert.equal(str.lastIndexOf("b", 2), 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ccc6d65084f3690a6b71bbafe37d41fa425c97a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Symbol primitive type represents a unique token + that can be used as a property key for an object. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let sym: symbol = Symbol(); +let obj1: object = { [sym]: "symbol" }; +type Key = keyof typeof obj1; +Assert.equal(obj1[sym as Key], "symbol"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..fd46477d62416dbf026ff3bbb94d4e11abdb64c6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The symbol keyword references the Symbol primitive type. + Symbol values are obtained using the global object 'Symbol' which has a number of methods and properties and can be invoked as a function. + options: + lib: es2019 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let sym: symbol = Symbol("NARC"); +let sym2: symbol = Symbol("TypeScript"); +Assert.equal(sym.description, "NARC"); +let s1: string = sym2.toString(); +let s2: symbol = sym.valueOf(); +Assert.equal(s1, "Symbol(TypeScript)"); +let flag1: boolean = false; +if (sym != sym2) { + flag1 = true; +} +Assert.isTrue(flag1); +let flag2: boolean = false; +if (sym == s2) { + flag2 = true; +} +Assert.isTrue(flag2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..24b14a5b4b562d1e749cafeff7f701720645fd73 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_symbol_type/the_symbol_type_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Symbol primitive type behaves as an object type with the same properties as the global interface type 'Symbol'. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x: symbol = Symbol(); +let y: any = {}; +y[x] = "primitive type"; +Assert.equal(y[x], "primitive type"); +y[Symbol.toStringTag] = "project"; +Assert.equal(y[Symbol.toStringTag], "project"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b3b814bdc11ba0034d309d8c3d879c2703143b6f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The Undefined type corresponds to the similarly named JavaScript primitive type and is the type of the undefined literal. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let u: undefined = undefined; +Assert.equal(u, undefined); +interface I2 { + s: number; +} +interface I { + a: number; + b?: string; + c?: number; + d?: object; + e?: string | number; + f?: I2; + g?: string & number; +} +let x: I = { a: 1 }; +Assert.isUndefined(x.b); +Assert.isUndefined(x.c); +Assert.isUndefined(x.d); +Assert.isUndefined(x.e); +Assert.isUndefined(x.f); +Assert.isUndefined(x.g); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2cf13d6f1442a6cccfd33db9d5082f7df691a077 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Void type, referenced by the void keyword, + represents the absence of a value and is used as the return type of functions with no return value. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let v: void = undefined; +function noReturn(): void { } +Assert.equal(v, undefined); +Assert.equal(noReturn(), undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..89495faeb293938ffdea23e938a96c442a7cd0fd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the only possible values for the Void type are null and undefined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let v: void; +Assert.equal(v, undefined); +let v2: void = undefined; +Assert.equal(v2, undefined); +let u: undefined = undefined; +v = u; +Assert.equal(v, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e90b5298b1ad70a5778935303b6763c82bbaccb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/primitive_types/the_void_type/the_void_type_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Void type is a subtype of the any type and a supertype of the null and undefined types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let v: void; +let u: undefined = undefined; +let a: any; +a = v; +Assert.equal(a, undefined); +v = u; +Assert.equal(v, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee4efbffa20f20a977b7ff97158df5b239a83910 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A signature's parameter list consists of zero or more required parameters, followed by zero or more optional parameters, + finally followed by an optional rest parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function fun1(firstParameter: string, lastParameter?: string) { + if (lastParameter) return firstParameter + " " + lastParameter; + else return firstParameter; +} +let result1 = fun1("Bob"); +let result3 = fun1("Bob", "Adams"); +Assert.equal(result1, "Bob"); +Assert.equal(result3, "Bob Adams"); +function fun2(firstParameter: string, ...restParameter: string[]) { + return firstParameter + " " + restParameter.join(" "); +} +let employeeName = fun2("Joseph", "Samuel", "Lucas", "MacKinzie"); +Assert.equal(employeeName, "Joseph Samuel Lucas MacKinzie"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b8c3e60d61e87e60a6629c5c1e4d0b06bad9021e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter declaration may specify either an identifier or a binding pattern. + The identifiers specified in parameter declarations and binding patterns in a parameter list must be unique within that parameter list. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +class Animal { + fname: string; + constructor(fname: string) { + this.fname = fname; + } +} +let name1 = "Anny"; +let animal = new Animal(name1); +name1 = "Bob"; +Assert.notEqual(animal.fname, name1); +let name2: "name2" = "name2"; +let animal2 = new Animal(name2); +Assert.equal(name2, animal2.fname); +const x: number = 3; +function fun(a: number, c = 1, b = x) { + return a + c + b; +} +let y = 1; +Assert.equal(y, 1); +let z = fun(y); +Assert.equal(z, 5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f88d8baf941c557303a0d05584ed3c266293f316 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the declaration includes a type annotation, the parameter is of that type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function ff(first: string, second: number) { + return first; +} +let first = "first"; +let second = 2; +ff(first, second); +Assert.equal(first, "first"); +Assert.equal(second, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c648383742497603f2e2a0f4cb30e713de1991f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the declaration specifies a binding pattern, + the parameter type is the implied type of that binding pattern. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +const x: number = 3; +function fun(a: number, c = 1, b = x) { + return a + c + b; +} +let y = 1; +Assert.equal(y, 1); +let z = fun(y); +Assert.equal(z, 5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..e3f047c52a667b4fa5e69edc634ed3233dd66e86 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the parameter is a rest parameter, the parameter type is any[]. + A type annotation for a rest parameter must denote an array type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function restFun(first: any, ...restname: any[]) { + return first + " " + restname.join(" "); +} +let restname1 = restFun("aa", "bb", "cc"); +Assert.equal(restname1, "aa bb cc"); +let restname2 = restFun(1, 2, 3, 4); +Assert.equal(restname2, "1 2 3 4"); +let restname3 = restFun(true, false, true); +Assert.equal(restname3, "true false true"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..f83717453c81a204180ff2132ce1b1890630f216 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter is permitted to include a public, private, + or protected modifier only if it occurs in the parameter list of a ConstructorImplementation (section 8.3.1) + and only if it doesn't specify a BindingPattern. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +class Person { + public name: string; + private age: number; + protected sex: string; + constructor(name: string, age: number, sex: string) { + this.name = name; + this.age = age; + this.sex = sex; + } + get _age() { + return this.age; + } + set _age(value) { + if (value >= 0) { + this.age = value; + } + } +} +class Child extends Person { + f() { + if (this.sex === "man") { + this.sex = "male"; + } else { + this.sex = "female"; + } + } +} +let a = new Child("wangwu", 15, "man"); +a.name = "lisi"; +a._age = 19; +Assert.equal(a.name, "lisi"); +Assert.equal(a._age, 19); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d3949692a8e3f3dc2926453ccdd98b41986fafdd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a call signature's return type annotation specifies the type of the value computed and returned by a call operation. + A void return type annotation is used to indicate that a function has no return value. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function returnNum(a: number, b: number): number { + return a + b; +} +let aa = returnNum(1, 2); +Assert.equal(aa, 3); +function returnString(name: string): string { + return name + " b!"; +} +let bb = returnString("rush"); +Assert.equal(bb, "rush b!"); +function returnBoolean(a: number, b: number): Boolean { + return a > b ? true : false; +} +let cc = returnBoolean(1, 2); +Assert.equal(cc, false); +function returnUndefine(a: undefined): undefined { + return a; +} +let ad: undefined; +let dd = returnUndefine(ad); +Assert.equal(dd, undefined); +function returnVoid(a: number): void { } +let ee = returnVoid(1); +Assert.equal(ee, null); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3068cbb81affdb5161f6c8cd3ad69464a6aad11 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a call signature with no return type annotation occurs in a context without a function body, + the return type is assumed to be the Any type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +let add: { (x: number, y: number): any }; +type anyT = ReturnType; +let x: anyT; +x = 1; +Assert.equal(x, 1); +x = "any"; +Assert.equal(x, "any"); +x = true; +Assert.equal(x, true); +x = undefined; +Assert.equal(x, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f13afb48bae9981a60b78919efc72390c69ccd48 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a call signature with no return type annotation occurs in a context that has a function body + (specifically, a function implementation, a member function implementation, or a member accessor declaration), + the return type is inferred from the function body. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +let add = function (x: number, y: number) { + return x + y; +}; +type Tadd = ReturnType; +let x: Tadd = 1; +Assert.equal(typeof x, "number"); +let sum = function (x: string, y: string) { + return x + y; +}; +type Tsum = ReturnType; +let y: Tsum = "hello"; +Assert.equal(typeof y, "string"); +let booltp = function (x: boolean) { + return true; +}; +let z = booltp(false); +Assert.equal(z, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..28ae71c53da59cd175d135c9b22de83408a5f4bd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Specialized signatures are used to express patterns where specific string values for some parameters + cause the types of other parameters or the function result to become further specialized. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +interface specialType { + Tfun(x: "hello"): "hello"; + Tfun(x: "world"): "world"; + Tfun(x: string): string; +} +class getType implements specialType { + Tfun(x: any): any { + let xx: "hello" = "hello"; + let xx2: "world" = "world"; + if (x === xx) { + return x; + } else if (x === xx2) { + return x; + } else if (typeof x === "string") { + return "isstring"; + } + } +} +let x1 = new getType(); +let xx1: "hello" = "hello"; +let y1: "hello" = x1.Tfun(xx1); +Assert.equal(xx1, y1); +let x2 = new getType(); +let xx2: "world" = "world"; +let y2 = x2.Tfun(xx2); +Assert.equal(xx2, y2); +let x3 = new getType(); +let y3 = x3.Tfun("helloworld"); +Assert.equal(y3, "isstring"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..317e140ce876bbb00b46e8a99f6ddc725e5b0d17 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type parameters in call signatures provide a mechanism for expressing the relationships of parameter and return types in call operations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function identity(x: T): T { + return x; +} +let x: number = 1; +Assert.equal(x, identity(x)); +let x2: "hello" = "hello"; +Assert.equal(identity(x2), "hello"); +function identity2(x: T, y: U): T { + return x; +} +Assert.equal(x, identity2(x, 1)); +Assert.equal(x2, identity2(x2, 1)); +function identity3(obj: T, key: K) { + return obj[key]; +} +let y = { h_a: 1, h_b: 2, h_c: 3, h_d: 4 }; +identity3(y, "h_a"); +function identity4(arg: T[]): T[] { + return arg; +} +let arg: number[] = [1, 2, 3]; +Assert.equal(arg, identity4(arg)); +let arg2: ["a", "b", "c"] = ["a", "b", "c"] +Assert.equal(arg2, identity4(arg2)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7e525144d5a3c4b216dbf5e3c25e0fad7d38af85 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type arguments for call signature type parameters may be explicitly specified in a call operation or may, + when possible, be inferred from the types of the regular arguments in the call. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../../suite/assert.js' + +function identity(x: T): T { + return x; +} +let x: number = 3; +let x2: "world" = "world"; +Assert.equal(identity(x), 3); +Assert.equal(identity(x2), "world"); +let y: string = "string"; +Assert.equal(identity(y), "string"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/construct_signatures/construct_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/construct_signatures/construct_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6c51a35674fd227d673a5558862b3fbd12c170c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/construct_signatures/construct_signatures.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A type may overload new operations by defining multiple construct signatures with different parameter lists. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal { + name: string | undefined; + age: number | undefined; + constructor(); + constructor(name: string); + constructor(age: number); + constructor(nameorage?: string | number, age?: number) { + if (typeof nameorage == "number") { + this.age = nameorage; + } + if (typeof nameorage == "string") { + this.name = nameorage; + } + if (age) { + this.age = age; + } + } +} +type AnimalConstructor = { + new(name: string, age: number): Animal; + new(name: string): Animal; + new(age: number): Animal; + new(): Animal; +}; +let AnimalConstructor: AnimalConstructor = Animal; +let tt1 = new AnimalConstructor(); +Assert.equal(tt1.age, undefined); +Assert.equal(tt1.name, undefined); +let tt2 = new AnimalConstructor("caihua2", 12); +Assert.equal(tt2.name, "caihua2"); +Assert.equal(tt2.age, 12); +let tt3 = new AnimalConstructor("caihua3"); +Assert.equal(tt3.name, "caihua3"); +Assert.equal(tt3.age, undefined); +let tt4 = new AnimalConstructor(1230); +Assert.equal(tt4.age, 1230); +Assert.equal(tt4.name, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/index_signatures/index_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/index_signatures/index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..20bdba48b2f4b722582ccb0e15ff1c0eb257e71b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/index_signatures/index_signatures.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + string index signatures, specified using index type string, define type constraints for all properties and numeric index signatures in the containing type. + numeric index signatures, specified using index type number, define type constraints for all numerically named properties in the containing type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x: { [key: string]: number } = { a: 97, A: 65 }; +x["b"] = 98; +Assert.equal(x["a"], 97); +Assert.equal(x["A"], 65); +let y: { [key: number]: boolean } = { 0: false, 1: true }; +y[-1] = true; +Assert.isBoolean(y[0]); +Assert.equal(y[-1], true); +interface StringKey { + [key: string]: string; +} +let z: StringKey = { "1": "0x01", "2": "0x02", 3: "0x03", "4": "0x04" }; +Assert.equal(z["1"], "0x01"); +Assert.equal(z["2"], "0x02"); +interface NumberKey { + [key: number]: string; +} +let u: NumberKey = { 1: "0x01", 2: "0x02", "3": "0x03", 4: "0x04" }; +Assert.equal(u[1], "0x01"); +Assert.equal(u[2], "0x02"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..81644f8a3d51b478f7ff4397e58080596ad27680 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_1.ts @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the PropertyName is followed by a question mark, the property is optional. + Only object type literals and interfaces can declare optional properties. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface point { + point(x: number, y: number, z?: number): number; +} +class P implements point { + point(x: number, y: number, z?: number | undefined): number { + if (z) return x + y + z; + return x + y; + } +} +let p1 = new P(); +let x: number = 1; +let y: number = 2; +let z: number = 3; +let sum = p1.point(x, y, z); +Assert.equal(sum, 6); +sum = p1.point(x, y); +Assert.equal(sum, 3); + +let point2: { x: number; y: number; z?: number }; +point2 = { x: 1, y: 2, z: 3 }; +let pp1 = point2; +Assert.equal(pp1.x, 1); +Assert.equal(pp1.y, 2); +point2 = { x: 0, y: 0, z: 0 }; +let pp2 = point2; +Assert.equal(pp2.x, 0); +Assert.equal(pp2.y, 0); +Assert.equal(pp2.z, 0); + +class B { + add(x: T, y: number): string; + add(x: string, y: U): string; + add(x: T, y: U): string; + add(x: T, y: U): string { + return x.toString() + y.toString(); + } +} +let b1 = new B(); +Assert.equal(b1.add(1, 2), '12'); +Assert.equal(b1.add("A", 2), 'A2'); +Assert.equal(b1.add("B", 3), 'B3'); +Assert.equal(b1.add(true, false), 'truefalse'); +b1.add = (x: string, y: U) => { return typeof x + typeof y; }; +Assert.equal(b1.add("B", 3), 'stringnumber'); + +class C { + add1?(x: number): string; + add2?(x: number, y: boolean): number | boolean; +} +let c1 = new C(); +c1.add1 = (x: number): string => { return (x * x).toString() } +Assert.equal(c1.add1(5), '25'); + +let c2 = new C(); +c2.add2 = (x: number, y: boolean) => { + if (y == true) { + return x; + } else { + return y; + } +} +Assert.equal(c2.add2(5, true), 5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..23960d17952ff4f1fecf3b106a9aad184c659fb8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/method_signatures/method_signatures_2.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A literal type may overload a method by declaring multiple method signatures + with the same name but differing parameter lists. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + add(x: number, y: number): number; + add(x: number, y: number, z: number): number; + add(x: any, y: any, z?: any): any { + if ( + typeof x == "number" && + typeof y == "number" && + typeof z == "undefined" + ) { + return x + y; + } + if ( + typeof x === "number" && + typeof y === "number" && + typeof z === "number" + ) { + return x + y + z; + } + } +} +let a1 = new A(); +Assert.equal(a1.add(1, 2), 3); +let a2 = new A(); +Assert.equal(a2.add(1, 2, 3), 6); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/property_signatures/property_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/property_signatures/property_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..626a7d8444c5b141afa4559061192a17cd1ea9bd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_members/property_signatures/property_signatures.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the PropertyName of a property signature must be unique within its containing type, and must denote a well-known symbol if it is a computed property name. + if the property name is followed by a question mark, the property is optional. Otherwise, the property is required. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let fname: { fristName: string, middleName?: string, lastName: string; } = { fristName: "NARC", lastName: "TypeScript" }; +let f2name: { fristName: string, middleName?: string, lastName: string; } = { + fristName: "Isaac", + middleName: "F", + lastName: "Newton", +}; +function fullName(name: { fristName: string, middleName?: string, lastName: string; }): string { + if (name.middleName != undefined) { + return name.fristName + " " + name.middleName + " " + name.lastName; + } else { + return name.fristName + " " + name.lastName; + } +} +let fn1: string = fullName(fname); +Assert.equal(fn1, "NARC TypeScript"); +let fn2: string = fullName(f2name); +Assert.equal(fn2, "Isaac F Newton"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..065f4113605e3fb80366c9951666339990848be2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An array type literal is written as an element type followed by an open and close square bracket. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: number[] = [2, 4, 6, 8]; +Assert.equal(arr[0], 2); +Assert.equal(arr[1], 4); +Assert.equal(arr[2], 6); +Assert.equal(arr[3], 8); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..09108a4f6380bb2490123c08cd937706ede87446 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_2.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An array type literal references an array type with the given element type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: (string | number | boolean)[] = ['s', 5, true]; +Assert.equal(arr[0], 's'); +Assert.equal(arr[1], 5); +Assert.equal(arr[2], true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..e02654d7c8702cc066ff101ed2b296dfe1ac9d8f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_3.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When union, intersection, function, or constructor types are used as array element types they must be enclosed in parentheses. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let test: string | number[]; +test = 'string'; +Assert.equal(test, 'string'); +test = [3, 5]; +Assert.equal(test[0], 3); +Assert.equal(test[1], 5); +let test2: (string | number)[]; +test2 = [2, 'a']; +Assert.equal(test2[0], 2); +Assert.equal(test2[1], 'a'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..eaf2c69edf06da85c81132590ade98d9f3a35e51 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/array_type_literals/array_type_literals_4.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Array types can be written using the 'Array' notation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: Array = [2, 'a']; +Assert.equal(arr[0], 2); +Assert.equal(arr[1], 'a'); +let x = function () { + return 'x'; +} +let y = function () { + return 'y'; +} +let fun: Array<() => string> = [x, y]; +Assert.isFunction(fun[0]); +Assert.isFunction(fun[1]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/constructor_type_literals/constructor_type_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/constructor_type_literals/constructor_type_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7841149ff16bdad00aed10de00f597a4775e62d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/constructor_type_literals/constructor_type_literals.ts @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A constructor type literal specifies the type parameters, regular parameters, and return type of a construct signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I1 { + x: number; + y: number; +} + +interface Constructor { + new(x: number, y: number): I1; +} +class I2 implements I1 { + readonly x: number; + readonly y: number; + + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} +function fun1( + cons: Constructor, + x: number, + y: number +): I1 { + return new cons(x, y); +} +let x1: I1 = fun1(I2, 2, 2) +Assert.equal(x1.x, 2); +Assert.equal(x1.y, 2); + +interface F1 { + x: T; + y: U; +} +interface ConstructorF { + new(x: T, y: U): F1; +} +class F2 implements F1 { + readonly x: T; + readonly y: U; + + constructor(x: T, y: U) { + this.x = x; + this.y = y; + } +} +function fun2( + cons: ConstructorF, + x: T, + y: U +): F1 { + return new cons(x, y); +} +let f1: F1 = fun2(F2, 1, true); +Assert.equal(f1.x, 1); +Assert.equal(f1.y, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..711f57f6cf0091e337d4f897435473c7eae22e0a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Function types with multiple call or construct signatures cannot be written as function type literals + but must instead be written as object type literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I { + (x: string): number; + (x: number): string +} +let i: I = Object.assign(function (x: any) { + if (typeof x === 'string') { + return x.toString(); + } else { + return x.toString(); + } +}) +Assert.equal(i('a'), 'a'); +Assert.equal(i(2), 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f4840816b8aa2c090ba66fa5dffe2df389287ce --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/function_type_literals/function_type_literals_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A function type literal specifies the type parameters, regular parameters, and return type of a call signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let fun1: (x: number, y: number) => number = + function (x: number, y: number): number { return x + y; }; +Assert.equal(fun1(1, 2), 3); +let fun2: Function = (x: number, y: number): boolean => { + return x > y ? true : false; +} +Assert.equal(fun2(1, 2), false); +interface Func { + fun(x: string): string; +} +let fun3: Func = { + fun(x: string): string { + return x; + } +} +Assert.equal(fun3.fun("aa"), "aa"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/intersection_type_literals/intersection_type_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/intersection_type_literals/intersection_type_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..daec49b722194cefc81cc474666bee392d22bb2d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/intersection_type_literals/intersection_type_literals.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An intersection type literal is written as a sequence of types separated by ampersands. + An intersection type literal references an intersection type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x: object & { name: string }; +x = { + name: 'intersection' +} +Assert.isObject(x); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/object_type_literals/object_type_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/object_type_literals/object_type_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c5de86e4b13dad39fe53f638cba8c64d77bbedf --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/object_type_literals/object_type_literals.ts @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An object type literal defines an object type by specifying the set of members that are statically considered to be present in instances of the type. + Object type literals can be given names using interface declarations but are otherwise anonymous. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let obj: { + num: number; + str: string; + boo: boolean; + fun: Function; +} = { + num: 5, + str: 'str', + boo: true, + fun(): number { + return 123; + } +} +Assert.equal(obj.num, 5); +Assert.equal(obj.str, 'str'); +Assert.equal(obj.boo, true); +Assert.equal(obj.fun(), 123); +interface I { + name: string, + age: number, + readonly id: number +} +let i: I = { + name: 'xiao', + age: 18, + id: 111 +} +Assert.equal(i.name, 'xiao'); +Assert.equal(i.age, 18); +Assert.equal(i.id, 111); +interface I2 { + name: string, + [oname: string]: string; +} +let i2: I2 = { + name: "join", + gender: "male" +}; +Assert.equal(i2.name, "join"); +Assert.equal(i2.gender, "male"); +class Myclass { + x: number = 1; + y: string = "aa"; + constructor() { } + fun(): number { + return 11; + } +} +let obj2: Myclass = new Myclass() +Assert.equal(obj2.fun(), 11); +Assert.equal(obj2.x, 1); +Assert.equal(obj2.y, "aa"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/predefined_types/predefined_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/predefined_types/predefined_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..fbf293ec2e2a97d5ffcd4b6f28b423e8dc643508 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/predefined_types/predefined_types.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The any, number, boolean, string, symbol and void keywords reference + the Any type and the Number, Boolean, String, Symbol, and Void primitive types respectively. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x1: number = 5; +Assert.equal(x1.toString(), "5"); + +let x2: boolean = true; +Assert.equal(x2.toString(), "true"); + +let x3: string = 's'; +Assert.equal(x3.toString(), "s"); + +let x4: symbol = Symbol(); +Assert.equal(x4.toString(), "Symbol()"); + +let x5: any; +x5 = 1; +x5 = "aa" +x5 = true; +Assert.equal(x5, true); + +let x6: void = undefined; +Assert.equal(x6, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..32e6a20d1fec4dee5c025a93e54cd5d333e37845 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Types are specified either by referencing their keyword or name, or by writing object type literals, + array type literals, tuple type literals, function type literals, constructor type literals, or type queries. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let num: number = 5; +Assert.isNumber(num); + +let obj = { + name: 'xiao', + age: 18 +} +Assert.equal(typeof obj, 'object'); + +let arr = [10, 5, 7, 20]; +Assert.equal(typeof arr, 'object'); + +let arr2 = ['str', 5, true]; +Assert.equal(typeof arr2, 'object'); + +let fun = (h_x: number, h_y: number) => { + return h_x + h_y +} +Assert.equal(typeof fun, 'function'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..861a9cc8cf66ab440802f181bfaf3fb9880d65b5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/specifying_types_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Parentheses are required around union, intersection, function, or constructor types when they are used as array element types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr: (string | number)[] = ['10', 5]; +Assert.equal(typeof arr, 'object'); +let m_fun = function func(x: number) { + return x; +} +let fun: ((h_x: string) => string) | ((h_x: number) => number) = m_fun; +Assert.equal(typeof fun, 'function'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/this_type_references/this_type_references_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/this_type_references/this_type_references_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..83357c76307a37a5f25736e2469d4dde51944a94 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/this_type_references/this_type_references_1.ts @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The meaning of a ThisType depends on the closest enclosing FunctionDeclaration, FunctionExpression, PropertyDefinition, ClassElement, + or TypeMember, known as the root declaration of the ThisType. + When the root declaration is an instance member or constructor of a class, the ThisType references the this-type of that class. + When the root declaration is a member of an interface type, the ThisType references the this-type of that interface. Otherwise, the ThisType is an error. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class myClass { + x: number; + y: number; + xy() { + return this.x * this.y; + } + m?: this; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} +let mc = new myClass(2, 5); +let mc0 = new myClass(1, 2); +mc.m = mc0; +Assert.equal(mc.m.x, 1); +Assert.equal(mc.m.xy(), 2); + +type PType = { A: T, B: T }; +interface Point { + x: number; + y: number; + PAdd?(a: this, b: this): this; + PObj?(a: this, b: this): PType; +} +let p1: Point = { x: 1, y: 2 }; +let p2: Point = { x: 2, y: 1 }; +let pa: Point = { + x: 0, + y: 0, + PAdd(a: Point, b: Point): Point { + let p: Point = { x: a.x + b.x, y: a.y + b.y }; + return p; + }, + PObj(a: Point, b: Point): PType { + let p: PType = { A: a, B: b }; + return p; + } +} +let p3: Point = pa.PAdd!(p1, p2); +let po: PType = pa.PObj!(p1, p2); +Assert.equal(JSON.stringify(p3), '{"x":3,"y":3}'); +Assert.equal(JSON.stringify(po), '{"A":{"x":1,"y":2},"B":{"x":2,"y":1}}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/tuple_type_literals/tuple_type_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/tuple_type_literals/tuple_type_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..5eba9508bde06dac11cc627305bee850f6625620 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/tuple_type_literals/tuple_type_literals.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A tuple type literal is written as a sequence of element types, separated by commas and enclosed in square brackets. + A tuple type literal references a tuple type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: [number, string, boolean] = [3, 'a', true] +Assert.isNumber(arr[0]); +Assert.isString(arr[1]); +Assert.isBoolean(arr[2]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_queries/type_queries_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_queries/type_queries_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b2d3b21886cf5930e10d167a4c62857109bd049d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_queries/type_queries_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type query obtains the type of an expression.A type query consists of the keyword typeof followed by an expression. + Type queries are useful for capturing anonymous types that are generated by letious constructs + such as object literals, function declarations, and namespace declarations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let a1: { x: number, y: string }; +let a2: typeof a1; +a2 = { + x: 10, + y: 'y' +} +Assert.equal(typeof a2, 'object'); + +function b1(x: number, y: number): number { return x + y; } +let b2: typeof b1; +b2 = (x: number, y: number): number => { return x - y; } +Assert.equal(typeof b2, 'function'); + +namespace CC { + export type C = keyof number & string; +} +let c1: CC.C; +let c2: typeof c1 = 'toExponential'; +c2 = 'toString'; +c1 = c2; +Assert.equal(c1, c2); + +let c3: CC.C; +let c4: typeof c3[] = ['toExponential', 'toFixed', 'toLocaleString', 'toPrecision', 'toString', 'valueOf']; +Assert.equal(JSON.stringify(c4), '["toExponential","toFixed","toLocaleString","toPrecision","toString","valueOf"]'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1fde37489d3fb7642d6e85693e23dccac7317e7a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type reference references a named type or type parameter through its name and, in the case of a generic type, supplies a type argument list. + A TypeReference consists of a TypeName that a references a named type or type parameter. A reference to a generic type must be followed by a list of TypeArguments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let obj: { + month: number; + day: number; +} = { + month: 5, + day: 15 +} +Assert.equal(obj.month, 5); +Assert.equal(obj.day, 15); +function identity1(value: T, message: U): [T, U] { + return [value, message]; +} +let identi = identity1(1, 'string'); +Assert.equal(identi[0], 1); +Assert.equal(identi[1], 'string'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..441d6416dd531195c598787d865a28a9652bbe74 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_2.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type reference to a generic type is required to specify exactly one type argument for each type parameter + of the referenced generic type, and each type argument must be assignable to the constraint of the corresponding type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I1 { + a: string; +} +interface I2 extends I1 { + b: string; +} +interface I3 extends I2 { + c: string; +} +interface I4 { + x: T; + y: U; +} + +let z: I4 = { + x: { a: 'a' }, + y: { + a: 'a', + b: 'b', + c: 'c' + } +} +Assert.equal(z.x.a, 'a'); +Assert.equal(z.y.a, 'a'); +Assert.equal(z.y.b, 'b'); +Assert.equal(z.y.c, 'c'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7d8838deab959eb8b345dde7eec6623d3f559749 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/type_references/type_references_3.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type argument is simply a Type and may itself be a type reference to a generic type. + A type reference to a generic type G designates a type wherein all occurrences of G's type parameters have been replaced + with the actual type arguments supplied in the type reference. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I1 { + a: string; +} +interface I2 extends I1 { + b: string; +} +interface I3 extends I2 { + c: string; +} +interface I4 { + x: T; + y: U; +} +let z: I4, I3> = { + x: { + x: { a: 'a' }, + y: { + a: 'a', + b: 'b' + } + }, + y: { + a: 'a', + b: 'b', + c: 'c' + } +} +Assert.equal(z.x.x.a, 'a'); +Assert.equal(z.x.y.b, 'b'); +Assert.equal(z.y.a, 'a'); +Assert.equal(z.y.b, 'b'); +Assert.equal(z.y.c, 'c'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/union_type_literals/union_type_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/union_type_literals/union_type_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..dd967d6d159600c6e1402c4cb269274652b7c8fb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/specifying_types/union_type_literals/union_type_literals.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A union type literal is written as a sequence of types separated by vertical bars. + A union type literal references a union type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let x: string | number | boolean; +x = 's'; +Assert.isString(x); +x = 5; +Assert.isNumber(x); +x = true; +Assert.isBoolean(x); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c5c1a672f8eea7b0af1def352a2f875a6f3f29f9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Any type is a supertype of all types, + and is assignable to and from all types. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: any; +x = 12; +Assert.equal(x, 12); +x = "abc"; +Assert.equal(x, "abc"); +x = true; +Assert.equal(x, true); +x = [1, 2, 3]; +Assert.equal(x[0], 1); +x = ["aa", "bb"]; +Assert.equal(x[0], "aa"); +x = (a: number, b: number) => { return a + b }; +Assert.isFunction(x); +x = Symbol("aa"); +Assert.isSymbol(x); +x = { a: 1, b: 1 }; +Assert.isObject(x); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d02ee66334469307b84f89f6e98242a265355b2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_2.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + in places where a type is not explicitly provided + and TypeScript cannot infer one, the Any type is assumed. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let a; +a = 25.25; +Assert.isNumber(a); +a = "narc"; +Assert.isString(a); +a = function add(a: any, b: any) { + return a + b; +}; +Assert.isFunction(a); +a = false; +Assert.isBoolean(a); +a = Symbol(); +Assert.isSymbol(a); +a = { 1408: "Movie" }; +Assert.equal(JSON.stringify(a), '{"1408":"Movie"}'); +a = null; +let flag = false; +if (a === null) { + flag = true; +} +Assert.isTrue(flag); +a = undefined; +Assert.isUndefined(a); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..77f7b25a7634b9d0fedfe89600652bf2025bea1d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_3.ts @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + properties of any name can be accessed through an Any value + and Any values can be called as functions or constructors with any argument list. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: any +let fun: any; +fun = (x: any) => x; +Assert.equal(fun(5), 5); +x = 1024; +x.toString(); +Assert.isString(x.toString()); +x = "AAA"; +x.length; +Assert.equal(x.length, 3); +Assert.equal(fun(x), "AAA"); +x = true; +Assert.equal(x, true); +Assert.equal(fun(x), true); +x = [1, 2, 3]; +Assert.equal(x[0], 1); +Assert.equal(fun(x), "1,2,3"); +x = ["aa", "bb"]; +Assert.equal(x[0], "aa"); +Assert.equal(fun(x), "aa,bb"); +x = (a: number, b: number) => { return a + b }; +Assert.isFunction(x); +Assert.isFunction(fun(x)); +x = Symbol("aa"); +Assert.isSymbol(x); +Assert.isSymbol(fun(x)); +x = { a: 1, b: 1 }; +Assert.isObject(x); +Assert.isObject(fun(x)); +class C { + public a: any; + public b: any; + constructor(a: any, b: any) { + this.a = a; + this.b = b; + } +} +let c1 = new C(1, "1"); +Assert.isNumber(c1.a); +let c2 = new C("aa", "bb"); +Assert.isString(c2.a); +let c3 = new C(true, true); +Assert.isBoolean(c3.a); +let c4 = new C({}, {}); +Assert.isObject(c4.a); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e0473c29b2b073669db5aacb7de77ab96057038 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/the_any_type/any_type_4.ts @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Any type is a supertype of all types, and is assignable to and from all types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: any +let y1: number; +x = 1024; +y1 = x; +Assert.equal(y1, 1024) +let y2: string; +x = "AAA"; +y2 = x; +Assert.equal(y2.length, 3); +let y3: boolean; +x = true; +y3 = x; +Assert.equal(y3, true); +let y4: number[]; +x = [1, 2, 3]; +y4 = x; +Assert.equal(y4[0], 1); +let y5: string[]; +x = ["aa", "bb"]; +y5 = x; +Assert.equal(y5[0], "aa"); +let y6: Function; +x = (a: number, b: number) => { return a + b }; +y6 = x; +Assert.equal(y6(1, 2), 3); +let y7: symbol; +x = Symbol("aa"); +y7 = x; +Assert.equal(y7.toString(), "Symbol(aa)"); +let y8: object; +x = { + toString() { + return 123; + } +}; +y8 = x; +Assert.isObject(x); +Assert.equal(y8.toString(), 123) \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6d5269259151a1dc16a6e1b53ab0c6a595c6f09a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type alias serves as an alias for the type specified in the type alias declaration. + A type alias declaration can introduce a name for any kind of type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type MyString = string; +let x: MyString = 'x'; +Assert.isString(x); + +type MyUnionType = number | string | boolean; +let y: MyUnionType = 10; +Assert.isNumber(y); +y = '10'; +Assert.isString(y); +y = true; +Assert.isBoolean(y); + +type MyInterType = object & { name: string }; +let z: MyInterType = { name: 'xiao' }; +Assert.equal(typeof z, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..351ed8fb63c14417e6355b5c58264bb684def232 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_2.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type alias may optionally have type parameters + that serve as placeholders for actual types to be provided when the type alias is referenced in type references. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type MyType = { + h_name: string; + h_age: number; + h_height?: number; + h_weight?: number +} +var h_ty: MyType = { + h_name: 'xiao', + h_age: 18, + h_height: 180 +} +Assert.equal(h_ty.h_height, 180); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d9e10e8b067b56e11a7b599b5d878d72201c53d5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_aliases/type_aliases_3.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type aliases are referenced using type references. + Type references to generic type aliases produce instantiations of the aliased type with the given type arguments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface I { + h_x: T; + h_y: U; +} +interface I2 { + h_y: T; + h_b: U; +} +type MyType = I | I2 +var mytest: MyType = { + h_x: 10, + h_y: 'y', + h_b: true +} +Assert.isString(mytest.h_y); + +type KeyOf = keyof T & keyof U; +let key1: KeyOf = 'toString'; +let key2: KeyOf = 'valueOf'; +Assert.equal(key1, 'toString'); +Assert.equal(key2, 'valueOf'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4fbf408a7acf884a37236ae252042d69555b247c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_1.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Every class and interface has a this-type that represents the actual type of instances of the class or interface + within the declaration of the class or interface. The this-type is referenced using the keyword this in a type + position. + within instance methods and constructors of a class, the type of the expression this is the this-type of the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface INTERFACE { + say: () => string; +} +let i: INTERFACE = { + say() { + return typeof this; + } +} +Assert.equal(i.say(), 'object'); +class C { + num: number; + constructor(num: number) { + this.num = num; + Assert.equal(typeof this, 'object'); + } +} +let h_c = new C(10); +Assert.equal(h_c.num, 10); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ff20e6e4366b01eda1c7663123a5d236ff6e6b11 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/this_types/this_types_2.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Classes and interfaces support inheritance and therefore the instance represented by this in a method + isn't necessarily an instance of the containing class—it may in fact be an instance of a derived class or interface. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class CA { + name: string = "CA"; + f() { + return this; + } + caName(cname: string) { + this.name = cname; + return "CA:" + this.name; + } +} + +class CB extends CA { + b() { + return this; + } + cbName(cname: string) { + this.name = cname; + return "CB:" + this.name; + } +} + +let bB: CB = new CB(); +let xB = bB.f().b(); +Assert.equal(bB, xB); +let thisStrB: string = bB.cbName("CB"); +Assert.equal(thisStrB, "CB:CB"); +let thisStrA: string = bB.caName("CA"); +Assert.equal(thisStrA, "CA:CA"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d0ef6684e44948c8b5ff8f93265da788cd50721d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type reference to a generic type must include + a list of type arguments enclosed in angle brackets and separated by commas. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +function identity1(value: T, message: U): [T, U] { + return [value, message]; +} +let identi = identity1(1, 2); +Assert.equal(identi[0], 1); +Assert.equal(identi[1], 2); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e2ef9e1ba175b7ae55e57768d1a429f267faebc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a call to a generic function may explicitly include a type argument list instead of relying on type inference. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface GenericIdentityFn { + (arg: T): T; +} +function identity(arg: T): T { + return arg; +} +let x: number = 1; +let y: GenericIdentityFn = identity; +let z = y(x); +Assert.equal(z, 1); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..933672c0697bf974a96be9dafbc0eec99276494f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type argument list is required to specify exactly one type argument for each corresponding type parameter, + and each type argument for a constrained type parameter is required to satisfy the constraint of that type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +function getname(name: T, number: Y): T { + return name; +} +interface Lengthwise { + length: number; +} +interface Search { + (name: T, number: Y): T; +} +let fn: Search = getname; +let aa: Lengthwise = { length: 11 }; +let a = fn("wan", aa); +Assert.equal(a, "wan"); +let fn2: Search = getname; +let bb: Lengthwise = { length: 33 }; +let b = fn2(22, bb); +Assert.equal(b, 22); +let fn3: Search = getname; +let cc: Lengthwise = { length: 44 }; +let c = fn3(true, cc); +Assert.equal(c, true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..36aed13d2cdc00936842417ebd602d240351276f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_argument_lists/type_argument_lists_4.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The process of substituting type arguments for type parameters in a generic type + or generic signature is known as instantiating the generic type or signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function fun(h_x: T): T { + return h_x; +} +let x = fun(10); +Assert.isNumber(x); +Assert.equal(x, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..542289e72a6a5b0f1231132bb863b7c2882ae477 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MinClass { + public list: T[] = []; + add(v: T) { + this.list.push(v); + } + findminNumberValue(): T { + let minNum = this.list[0]; + + for (let i = 0; i < this.list.length; i++) { + if (minNum > this.list[i]) { + minNum = this.list[i]; + } + } + return minNum; + } +} + +let minStringValue = new MinClass(); +minStringValue.add("a"); +minStringValue.add("b"); +minStringValue.add("c"); +minStringValue.add("d"); +Assert.equal(minStringValue.findminNumberValue(), "a"); + +let minNumberValue = new MinClass(); +minNumberValue.add(1); +minNumberValue.add(2); +minNumberValue.add(3); +minNumberValue.add(4); +Assert.equal(minNumberValue.findminNumberValue(), 1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f82c9044bdf2fd53ee31de6a4c51088c9f19caa --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface OutputValue { + (arg: T): T; +} +let x: OutputValue = function (arg: T): T { + return arg; +}; +Assert.equal(x("abc"), "abc"); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ac5683c0674a1a5d2ad80ddd24b911bf4ddf576 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + name: string; + age: number; + job: string; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} + +type optionallyTest = { + [P in keyof T]?: T[P]; +}; +let x: optionallyTest = {}; +x = { name: "dog" }; +Assert.isUndefined(x.job); +Assert.isUndefined(x.age); +x = { name: "dog", age: 18 }; +Assert.isUndefined(x.job); +x = { name: "dog", age: 18, job: "teacher" }; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb7eaf98f37296ab1b011d9b005aa9de376ade38 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +function returnAnyTypeValue(a: T): T { + return a; +} +Assert.equal(returnAnyTypeValue(1), 1); +Assert.equal(returnAnyTypeValue("char"), "char"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..b72720d1126649c738ffb4bf49296f7c98368369 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type parameter names must be unique. A compile-time error occurs + if two or more type parameters in the same TypeParameterList have the same name. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function sum(a: T, b: K): string { + return "a"; +} +Assert.equal(sum(1, 1), "a"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dee8cfec5309f7db1585404c75d310c18664eee --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type parameter may have an associated type parameter constraint that establishes an upper bound for type arguments. + Type parameters may be referenced in type parameter constraints within the same type parameter list, + including even constraint declarations that occur to the left of the type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +type Dog = { + m_name: string; + m_age: number; + m_job: string; +}; +type PickProperty = { [P in K]: T[P] }; +type test = PickProperty; +let cc: test = { + m_age: 20, + m_name: "wangwang", +}; +Assert.isNumber(cc.m_age); +Assert.isString(cc.m_name); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity.ts new file mode 100644 index 0000000000000000000000000000000000000000..9656503fa3207cf19b1b330561f7f9cfe5470503 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the variables 'a' and 'b' are of identical types because the two type references to 'C' create types with a private member 'x' that originates in the same declaration, + and because the two private 'x' members have types with identical sets of members once the type arguments 'X' and 'Y' are substituted. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Teacher { + private x: T; + constructor(t: T) { + this.x = t; + } + +} + +interface student1 { f(): string; } + +interface student2 { f(): string; } + +class Person implements student1, student2 { + f() { + return "X+Y" + } +} + +let a: Teacher = new Teacher(new Person); +let b: Teacher = new Teacher(new Person); +Assert.isObject(a); +Assert.isObject(b); +Assert.equal(typeof a, typeof b) \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8057eac9664d59a889eae02e25776df8f69cc769 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two types are considered identical when they are both the Any type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = any; +type U = any; + +type IsEqual = + (() => T1 extends TT ? 1 : 2) extends + (() => T2 extends UU ? 1 : 2) + ? true + : false + +let a1: T = 10; +let b1: U = 10; +Assert.equal(typeof a1, typeof b1); +let a2: T = 10; +let b2: U = 10; +Assert.equal(typeof a2, typeof b2); + +let isEqual: IsEqual = true; +Assert.isTrue(isEqual); + +let a3: T = 'a3'; +let b3: U = 1024; +a3 = b3; +Assert.equal(a3, b3); +a3 = 'A3'; +b3 = false; +b3 = a3; +Assert.equal(b3, a3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0aae0021bcf6751fd058ef09f8503eecd7ed2539 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_2.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two types are considered identical when they are the same primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = number; +type U = number; + +type IsEqual = + (() => T1 extends TT ? 1 : 2) extends + (() => T2 extends UU ? 1 : 2) + ? true + : false + +let a: T = 5; +let b: U = 10; +Assert.equal(typeof a, typeof b); + +let isEqual1: IsEqual = true; +let isEqual2: IsEqual = true; +Assert.equal(isEqual1, isEqual2); +Assert.isTrue(isEqual1); + +let a1: T = 1024; +let b1: U = 999; +a1 = b1; +Assert.equal(a1, b1); +a1 = 37; +b1 = 111; +b1 = a1; +Assert.equal(b1, a1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..9cb462699eb183d63e8577d008793761795c3a4f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two types are considered identical when they are the same type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = string; +type U = string; +function func(arg: T) { + return arg; +} + +let a = func('a'); +let b = func('b'); +Assert.equal(typeof a, typeof b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c40674016dcc4c8e829f24f70c2d625517aa24a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two types are considered identical when they are union types with identical sets of constituent types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = number | string | boolean; +type U = number | string | boolean; + +type IsEqual = + (() => T1 extends TT ? 1 : 2) extends + (() => T2 extends UU ? 1 : 2) + ? true + : false + +let a1: T = 10; +let a2: U = 5; +Assert.equal(typeof a1, typeof a2); +let b1: T = '10'; +let b2: U = '5'; +Assert.equal(typeof b1, typeof b2); +let c1: T = true; +let c2: U = false; +Assert.equal(typeof c1, typeof c2); + +let isEqual: IsEqual = true; +Assert.isTrue(isEqual); + +let d1: T = true; +let d2: U = 555; +d1 = d2; +Assert.equal(d1, d2); +d1 = 'd1'; +d2 = false; +d2 = d1; +Assert.equal(d2, d1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa1e6dce8b3285ca9ad5f257558e818756044218 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_5.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two members are considered identical when they are public properties with identical names, optionality, and types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface M{ + str: string; + mem?: number; +} +interface M{ + str: string; + mem?: number; + boo: boolean; +} +let a: M = { + str: 'a', + mem: 5, + boo: true +} +let b: M = { + str: 'a', + mem: 5, + boo: false +} +Assert.equal(typeof a.str, typeof b.str); +Assert.equal(typeof a.mem, typeof b.mem); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..e545055f5e2daf40089c10fee5be4155ac10bd67 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/Type_and_Member_Identity/Type_and_Member_Identity_6.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Two call or construct signatures are considered identical + when they have the same number of type parameters with identical type parameter constraints and, + after substituting type Any for the type parameters introduced by the signatures, + identical number of parameters with identical kind and types, and identical return types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface M{ + (arg: T): T; +} +interface M{ + (arg: T): T; +} +function func(arg: T): T{ + return arg; +} +let a: M = func('a'); +Assert.isString(a); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d3280708cca6eaa75708d031506ce1df84ba19b6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The apparent members of the primitive type Number and all enum types are the apparent members of the global interface type 'Number'. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +let num: Number = 1; +enum Color { + red, + black, + green +} +let c: Color.black = Color.black; +num.toString(); +c.toString(); +Assert.equal(num.valueOf(), c.valueOf()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a84a2174f91884f984d886fd39be9dbfe46a9e0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/apparent_members/apparent_members_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The apparent members of the primitive type Boolean are the apparent members of the global interface type 'Boolean'. + The apparent members of the primitive type String and all string literal types are the apparent members of the global interface type 'String' + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +let bool: Boolean = true; +Assert.isBoolean(bool.valueOf()) + +let str1: String = "string"; +type color = "RED" | "BLUE" | "BALCK" +let str2: color = "BLUE"; +str1.toString; +str2.toString; +Assert.equal(typeof str1.valueOf(), typeof str2.valueOf()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..40620e944a2b4fd080fc30d63919b53dc4cda46b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S and T are identical types. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: string +} +interface S { + name: string +} +let t: T; +let s: S = { name: 'xiao' } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..11ac500ff8e63d674dee026e47245538b61dd464 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is an intersection type and S is assignable to each constituent type of T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface Foo { + name: string +} +interface Bar { + age: number +} +interface S { + name: string + age: number +} +type T = Foo & Bar +let t: T +let s: S = { + name: 'xiao', + age: 18 +} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..02f551bf69fc9be878a2f748c4ef89035f49174d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a type parameter and the constraint of S is assignable to T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface event { + data: T +} +type T = event +type S = event +let t: T +let s: S = { data: 10 } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..f26c7fdb3d25331daa672a1f83946b390a9fb8f6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a property and S has an apparent property N where + M and N have the same name, + the type of N is assignable to that of M, + if M is a required property, N is also a required property, and + M and N are both public, M and N are both private and originate in the same declaration, + M and N are both protected and originate in the same declaration, + or M is protected and N is declared in a class derived from the class in which M is declared. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +type T = { + name: any + age: number +} +type S = { + name: string + age: number +} +let t: T +let s: S = { + name: "Xi", + age: 20, +} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..a058d9e4af91380c1815fbe597b6b07aa40e7975 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, + when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any), + the signatures are of the same kind (call or construct), + M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M, + for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, + and the result type of M is Void, or the result type of N is assignable to that of M. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + (x: any, y: any): void +} +interface S { + (x: number): void +} +let t: T = (x, y): void => { + return; +} +let s: S = (x): void => { + return; +} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..4500361cbce6ed9b99409dfa0411f77fa4741909 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is an optional property and S has no apparent property of the same name as M. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name?: string + age?: number +} +interface S { + name?: string + age?: number +} +let t: T +let s: S = { name: 'xiao' } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..024a7d9b7b353f7199c07977a99c6d9442ceb9b0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a string index signature of type U, + and U is the Any type or S has an apparent string index signature of a type that is assignable to U. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface U { + anything: string +} +interface T { + [key: string]: U +} +interface S { + [keyname: string]: U +} +let t: T = {} +let s: S = {} +s['obj'] = { anything: "thing" } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts new file mode 100644 index 0000000000000000000000000000000000000000..179e0efd85b36bcc50ec1cb59f7e6de72e102095 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a numeric index signature of type U, + and U is the Any type or S has an apparent string or numeric index signature of a type that is assignable to U. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface U { + anything: string +} +interface T { + [key: number]: U +} +interface S { + [keyname: number]: U +} +let t: T = {} +let s: S = {} +s[10] = { anything: "thing" } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7a36cfa23c311d4c3f7ebb20d6a7133cbaac156 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The assignment compatibility and subtyping rules differ only in that, + an object type without a particular property is assignable to an object type in which that property is optional. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name?: string + age?: number +} +interface S { +} +let t: T +let s: S = {} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts new file mode 100644 index 0000000000000000000000000000000000000000..119ae5e2919a9be2bbe0f3e5e63ad0977fa5fe45 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when assigning values or passing parameters, + optional properties must either be present and of a compatible type, or not be present at all. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function func(stu: { num: number; name?: string }) { + return stu +} +let f1 = func({ num: 1234 }); +Assert.equal(JSON.stringify(f1), '{"num":1234}') + +let f2 = func({ num: 1234, name: "xiao" }) +Assert.equal(JSON.stringify(f2), '{"num":1234,"name":"xiao"}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7d9366ad681ee8ff549e4349d74ca60fe13d436 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S or T is the Any type. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: string +} +let t: T +const S: any = 'str' +t = S +Assert.equal(t, S); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b3ef6c5e96c7c844e889913441618595c3eab9a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is the Undefined type. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: undefined +} +interface S { + name: undefined +} +let t: T +let s: S = { name: undefined } +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b11adc97e66b4cdcc317e08b8ab4fa7c9ef62e3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is the Null type and T is not the Undefined type. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +let T: any; +const S: null = null; +T = S; +Assert.equal(T, S); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..9dacbcde775607e423b408a0f3355997e0ff3db3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S or T is an enum type and the other is the primitive type Number. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +enum T { + A, + B, + C +} +let S: number = 5 +let t: T +t = S +Assert.equal(t, S); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ccdf7dc277002269813f9530f957c0aabc32966 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a string literal type and T is the primitive type String. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let T: string = "T"; +type S = "s" | "str" | "string"; +let s: S = "str" +T = s +Assert.equal(T, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b3508a87885c90fb1f99ced6fc7cd4a6d73426a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a union type and each constituent type of S is assignable to T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string + name: string +} +type S = Foo | Bar +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} +Assert.equal(s.name, 'S'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d5a56d4d0eceb4c05944a73bb3e93b373c38e46 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is an intersection type and S is assignable to each constituent type of T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: string + age: number +} +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string +} +type S = Foo & Bar +let t: T; +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..bfc0b3f5416ebd2a0e69efdd2902d5c07e6a43b1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is a union type and S is assignable to at least one constituent type of T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface Foo { + name: string +} +interface Bar { + age: number +} +interface S { + age: number +} +type T = Foo | Bar +let t: T +let s: S = { + age: 18 +} +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts new file mode 100644 index 0000000000000000000000000000000000000000..88f45984e2ed17b99bd21c7d3dc10e3214ddf79d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + During type argument inference in a function call, + it is in certain circumstances necessary to instantiate a generic call signature of an argument expression in the context of a non-generic call signature of a parameter such that further inferences can be made. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function fun(x: S[], y: T[], combine: (x: S) => (y: T) => U): U[] { + let len = Math.max(x.length, y.length); + let result: U[] = []; + for (let i = 0; i < len; i++) result.push(combine(x[i])(y[i])); + return result; +} + +let names = ["Peter", "Paul", "Mary"]; +let ages = [7, 9, 12]; +let pairs = fun(names, ages, s => n => ({ name: s, age: n })); +Assert.equal(JSON.stringify(pairs), '[{"name":"Peter","age":7},{"name":"Paul","age":9},{"name":"Mary","age":12}]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6a809181c5dc38184e0829e6be1cc5450b771ece --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type contains only optional properties, without the excess property check, + any object literal would be assignable to the variable. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I { + a?: number + b?: boolean + c?: string +} +let x: I = { + a: 12, + b: true, + c: "ccc" +} +Assert.equal(x.a, 12); +Assert.equal(x.b, true); +Assert.equal(x.c, "ccc"); +let y: I = { + a: 11, + b: false +} +Assert.equal(y.a, 11); +Assert.equal(y.b, false); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1d2b4cdcc4f5c7e7930b29dfb272907fe303295a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/excess_properties/excess_properties_2.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not c this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In cases where excess properties are expected, an index signature can be added to the target type as an indicator of intent. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I { + a: string; + b?: boolean; + [str: string]: any; +} + +let x: I = { + a: "aaa", + b: true, + c: "ccc", + d: "ddd" +}; +Assert.equal(JSON.stringify(x), '{"a":"aaa","b":true,"c":"ccc","d":"ddd"}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/recursive_types/recursive_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/recursive_types/recursive_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab23d074cfd19ec5911990e01f44730731e6a749 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/recursive_types/recursive_types.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when comparing types S and T for a given relationship, + the relationship in question is assumed to be true for every directly or indirectly nested occurrence of the same S and the same T + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface iA { + next: iA; +} +interface iB { + next: iC; +} +interface iC { + next: iD; +} +interface iD { + next: iB; +} +type IsEqual = + (() => T1 extends T ? 1 : 2) extends + (() => T2 extends U ? 1 : 2) + ? true + : false +var t1: IsEqual = true; +Assert.isTrue(t1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..10ca7b99b978f50598f858c52fd08bcc84477aa4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, S and T are identical types. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: string + age: number +} +interface S { + name: string + age: number + hobbies: string[] +} +let t: T = { + name: "T", + age: 20 +}; +let s: S = { + name: "S", + age: 19, + hobbies: ["draw", "game"] +}; + +t = s +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..9ee1b09a00f92bcc4d599245a85a9f992096756f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and T is an intersection type and S is a subtype of each constituent type of T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface S { + name: string + age: number + height: number +} +interface Foo { + name: string +} +interface Bar { + age: number +} +type T = Foo & Bar +let s: S = { + name: "T", + age: 18, + height: 180 +} +let t: T = { + name: "S", + age: 20 +} + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..51fb1ee827563c6fd1be8e25f59d1c4ca93ff819 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a type parameter and the constraint of S is a subtype of T. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +let test = (obj: T, key: S): any => { + return obj[key]; +} +let obj = { + a: 'a', + b: 'b' +} +let res = test(obj, "a"); +Assert.equal(res, 'a'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb4b90c5a256f8fec9c2ab86afb2aafbd7efc794 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, + and for each member M in T, M is a property and S has an apparent property N where, + M and N have the same name, + the type of N is a subtype of that of M, + if M is a required property, N is also a required property, and + M and N are both public, M and N are both private and originate in the same declaration, + M and N are both protected and originate in the same declaration, + or M is protected and N is declared in a class derived from the class in which M is declared. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = { + name: any + age: number +} +type S = { + name: string + age: number + height: number +} +let t: T = { + name: "xiao", + age: 19 +} +let s: S = { + name: "Xi", + age: 20, + height: 180 +} + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..665f1fb45e797f7e33429fa223c456e95875ff8f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, + when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any), + and for each member M in T, M is a property and S has an apparent property N where, + the signatures are of the same kind (call or construct), + M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M, + for parameter positions that are present in both signatures, each parameter type in N is a subtype or supertype of the corresponding parameter type in M, + and the result type of M is Void, or the result type of N is a subtype of that of M. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + (x: any, y: any): void +} +interface S { + (x: number): void +} +let t: T = (x, y): void => { + return; +} +let s: S = (x): void => { + return; +} + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..098eaa16753a8830ba02685e70c7cab233e4c121 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and T is the Any type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + name: any + age: any +} +interface S { + name: string + age: number + hobbies: string[] +} +let t: T = { + name: "T", + age: 20 +}; +let s: S = { + name: "S", + age: 19, + hobbies: ["draw", "game"] +}; + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3faad4a7ce1265081ea30b87e773df0ee931efbb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T and S is the Undefined type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + name: string | undefined + age: number | undefined +} +interface S { + name: undefined + age: undefined + hobbies: undefined +} +let t: T = { + name: "T", + age: 20 +}; + +let s: S = { + name: undefined, + age: undefined, + hobbies: undefined +}; + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..45fb7807246efc589a93d8c405324077987fbde6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and S is the Null type and T is not the Undefined type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + name: any + age: any +} +interface S { + name: null + age: null + hobbies: null +} +let t: T = { + name: "T", + age: 20 +}; + +let s: S = { + name: null, + age: null, + hobbies: null +}; + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..7452d54fd8ff995957e17b21b1613378bf2d2ce9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and S is an enum type and T is the primitive type Number. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var T: number = 4; +enum S { + Mon = 1, + Tue, + Wde, + Thur, + Fri +} + +T = S.Mon; +Assert.equal(T, S.Mon); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..03ee7dd0d92fbcef30b5708a0bd7437a49c09c93 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a string literal type and T is the primitive type String. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var T: string = "T"; +type S = "s" | "str" | "string"; +let s: S = "str" + +T = s; +Assert.equal(T, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..8562babf4a7196a8d21ea6f8d1f947a9733c920c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a union type and each constituent type of S is a subtype of T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let T: any = true; +let S: number | string = 18; + +T = S; +Assert.equal(T, 18); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..b713d707f56fb0183cecef776317ba007a31f385 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an intersection type and at least one constituent type of S is a subtype of T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface T { + name: string + age: number +} +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string +} +type S = Foo & Bar +let t: T = { + name: "T", + age: 18 +} +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} + +t = s; +Assert.equal(t, s); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..f775365370ab5559a7e6c828a6a9c853e2582509 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and T is a union type and S is a subtype of at least one constituent type of T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let T: number | string = "18"; +let S: number = 20; + +T = S; +Assert.equal(T, S); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..39e9d6a0b43dc59fdbf0c7ec5848bf5e85c1a6e5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In certain contexts, + inferences for a given set of type parameters are made from a type S, in which those type parameters do not occur, + to another type T, in which those type parameters do occur. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface I1 { + (a: number): void +} +interface I2 { + (cb: I1): void +} +const fn: I2 = function (cb) { + Assert.isFunction(cb); +} + +fn(function (a) { + Assert.isNumber(a); + a = a + 1; +}); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a07b9b18a679b392126e00e80bf4023196612b7c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are references to the same generic type, + inferences are made from each type argument in S to each corresponding type argument in T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function map(a: T[], f: (x: T) => U): U[] { + var result: U[] = []; + for (var i = 0; i < a.length; i++) result.push(f(a[i])); + return result; +} + +var names = ["Peter", "Paul", "Mary"]; + +var lengths = map(names, s => s.length); +Assert.equal(typeof lengths, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..819c983fbf8c95e16fde6e4043cb42fda6d2f86a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are tuple types with the same number of elements, + inferences are made from each element type in S to each corresponding element type in T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + + +let T = [1, 'a', true] +Assert.equal(typeof T, 'object') + +let S = [[1, 2, 3], 2, 'b'] +Assert.equal(typeof S, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..0e0a4d5b7be83436f6a43c56a7bdae4e290dadbf --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_4.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if T is a union or intersection type: + First, inferences are made from S to each constituent type in T + that isn't simply one of the type parameters for which inferences are being made. + If the first step produced no inferences then + if T is a union type and exactly one constituent type in T is simply a type parameter for which inferences are being made, + inferences are made from S to that type parameter. + Otherwise, if S is a union or intersection type, inferences are made from each constituent type in S to T. + module: ESNext + isCurrent: true + ---*/ + +import { Assert } from '../../../../../suite/assert.js' + + +type Maybe = T | void +function isDefined(x: Maybe): x is T { + return x! == undefined && x !== null; +} +function isUndefined(x: Maybe): x is void { + return x === undefined || x === null; +} +function getOrElse(x: Maybe, defaultValue: T): T { + return isDefined(x) ? x : defaultValue; +} +function test1(x: Maybe) { + let x1 = getOrElse(x, "Undefined"); + Assert.isString(x1) + let x2 = isDefined(x) ? x : "undefined"; + Assert.isString(x2) + let x3 = isUndefined(x) ? "Undefined" : x; + Assert.isString(x3) +} +test1('t1') +function test2(x: Maybe) { + let x1 = getOrElse(x, - 1); + Assert.isNumber(x1) + let x2 = isDefined(x) ? x : -1; + Assert.isNumber(x2) + let x3 = isUndefined(x) ? -1 : x; + Assert.isNumber(x3) +} +test2(5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f5580fb5c59418b58d6676491852a343b91f37f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_5.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + If M is a property and S contains a property N with the same name as M, + inferences are made from the type of N to the type of M. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + name: any +} +interface S { + name: string +} +var t: T +var s: S = { name: "xxx" } +t = s + +Assert.isString(t.name); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f02f24664ea277cdad33d7086ab949436179fec --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_6.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + if M is a string index signature and S contains a string index signature N, + inferences are made from the type of N to the type of M. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface S { + [value: string]: string +} +let s: S = { + 'name': 'xiao' +} +Assert.equal(typeof s, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..535f582d80b04997668546d82e2cf7fb93ca16f1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/type_relationships/type_inference/type_inference_7.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + if M is a numeric index signature and S contains a numeric index signature N, + inferences are made from the type of N to the type of M. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../../suite/assert.js' + +interface T { + [key: number]: string +} +let S: { [value: number]: T } = {} +Assert.equal(typeof S, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..47db9b2c1a5036872ed04443780e459aa1de59d7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_1.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + union types represent values that may have one of several distinct representations. + a value of a union type A | B is a value that is either of type A or type B. Union types are written using union type literals + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var x: number | string; +var y: boolean | string; +x = 2048; +Assert.isNumber(x); +y = false; +Assert.isFalse(y); +x = "NARC"; +Assert.isString(x); +y = "1"; +Assert.equal(y, "1"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f2c050785d5497d9fcbac68cc60deba322e55ca7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a union type encompasses an ordered set of constituent types. + while it is generally true that A | B is equivalent to B | A, the order of the constituent types may matter when determining the call and construct signatures of the union type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: number | string; +let y: string | number; +x = 1408; +y = 1408; +Assert.equal(x, y); +Assert.equal(typeof x, typeof y); +x = "Shift"; +y = "Shift"; +Assert.equal(x, y); +Assert.equal(typeof x, typeof y); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1a66085107ba9c97d4a3513d3c9c7bb9197e03f2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_3.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a union type U is a subtype of a type T if each type in U is a subtype of T. + a type T is a subtype of a union type U if T is a subtype of any type in U. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class ClassT { + size: number = 0; + description: string = ''; +} +class ClassU1 extends ClassT { + alive: boolean = false; +} +class ClassU2 extends ClassT { + weight: number = 0; +} +interface InterfaceU1 { + speak(): string; +} +interface InterfaceU2 { + eat(): string; +} +class ClassT2 implements InterfaceU1, InterfaceU2 { + food: string = ''; + language: string = ''; + speak() { + return this.language; + } + eat() { + return this.food; + } + constructor(food: string, language: string) { + this.food = food; + this.language = language; + } +} +let u1: ClassU1 | ClassU2 = { size: 7, description: "A", alive: false }; +let t1: ClassT; +t1 = u1; +Assert.equal(JSON.stringify(t1), '{"size":7,"description":"A","alive":false}'); +let u2: InterfaceU1 | InterfaceU2; +let t2: ClassT2 = new ClassT2("rice", "Chinese"); +u2 = t2; +Assert.equal(JSON.stringify(u2), '{"food":"rice","language":"Chinese"}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..d87814f638344da645239e1e0404a46ac37dad70 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a union type U is assignable to a type T if each type in U is assignable to T. + a type T is assignable to a union type U if T is assignable to any type in U. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type numType = { num: number }; +type strType = { str: string }; +type boolType = { bool: boolean }; +type objType = { obj: Object }; +let x: any; +let y: numType | strType | boolType | objType | undefined; +let z: numType = { num: 0xCA }; +x = z; +Assert.equal(JSON.stringify(x), '{"num":202}'); +let a: strType = { str: "QWER" }; +x = a; +Assert.equal(JSON.stringify(x), '{"str":"QWER"}'); +let b: boolType = { bool: false }; +x = b; +Assert.equal(JSON.stringify(x), '{"bool":false}'); +let c: objType = { obj: { 0: "ZERO" } }; +x = c; +Assert.equal(JSON.stringify(x), '{"obj":{"0":"ZERO"}}'); +y = { num: 0xCA, str: "ABC", bool: false, obj: c }; +x = y; +Assert.equal(JSON.stringify(x), '{"num":202,"str":"ABC","bool":false,"obj":{"obj":{"0":"ZERO"}}}'); +y = z; +Assert.equal(JSON.stringify(y), '{"num":202}'); +y = a; +Assert.equal(JSON.stringify(y), '{"str":"QWER"}'); +y = b; +Assert.equal(JSON.stringify(y), '{"bool":false}'); +y = c; +Assert.equal(JSON.stringify(y), '{"obj":{"0":"ZERO"}}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b128e80e9bf130b72112c0aa3261a7e53eb4f41 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_5.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the || and conditional operators may produce values of union types, and array literals may produce array values that have union types as their element types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: A1 = 1408 || "NARC"; +let y: string | number; +x = 1500; +y = x <= 1408 ? "NARC" : 1024; +Assert.equal(y, 1024); +x = 100; +y = x <= 1408 ? "NARC" : 1024; +Assert.equal(y, "NARC"); +type A1 = string | number | object; +type B1 = number | boolean | string; +let z: A1 & B1; +z = 125; +Assert.isNumber(z); +z = "Fn"; +Assert.isString(z); +let a: Array | Array; +a = [0, true, -1, false]; +Assert.equal(JSON.stringify(a), '[0,true,-1,false]'); +a = [true, "True", false, "False"]; +Assert.equal(JSON.stringify(a), '[true,"True",false,"False"]'); +let b: number[] | boolean[]; +b = [2, 4, 6]; +Assert.equal(JSON.stringify(b), '[2,4,6]'); +b = [true, false]; +Assert.equal(JSON.stringify(b), '[true,false]'); +let c: (number | string)[] | (boolean | Object)[]; +c = [1, 3, 5, "AND", "OR"]; +Assert.equal(JSON.stringify(c), '[1,3,5,"AND","OR"]'); +c = [true, false, { 0x00: "0x00" }, { 0xFA: "0xFA" }]; +Assert.equal(JSON.stringify(c), '[true,false,{"0":"0x00"},{"250":"0xFA"}]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..171bc00ed48afe9b0c3264b642b29ed4151f8f2b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_6.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + type guards may be used to narrow a union type to a more specific type. + in particular, type guards are useful for narrowing union type values to a non-union type values. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: string | number; +x = 37; +x = "hello"; +let h_n = typeof x === "string" ? x.length : x; +Assert.isNumber(h_n); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..885163cdc92a5485cea8a46d4b96b56e523e0c2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/union_types/union_types_7.ts @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + for purposes of property access and function calls, the apparent members of a union type are those that are present in every one of its constituent types, + with types that are unions of the respective apparent members in the constituent types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type numType = { num: number }; +type strType = { str: string }; +type boolType = { bool: boolean }; +type objType = { obj: Object }; +type NS = numType | strType; +type OB = objType | boolType; +var nsv: NS = { num: 1024, str: "NS" }; +var obv: OB = { bool: true, obj: { 0xFF: "0xFF" } }; +var nsobv_1: NS | OB = { num: 1024, obj: { 0xFF: "0xFF" } } +var nsobv_2: NS | OB = { bool: false, str: "nsobv_2" }; +var nsobv_3: NS | OB = { num: 114, bool: false, str: "nsobv_3", obj: { 0xAF: "0xAF" } } +Assert.equal(typeof nsv, "object"); +Assert.equal(typeof obv, "object"); +Assert.equal(typeof nsobv_1, "object"); +Assert.equal(typeof nsobv_2, "object"); +Assert.equal(typeof nsobv_3, "object"); +interface T1 { + x: number, + y: string, + z: boolean +} +interface T2 { + x: string, + y: string, + z: string +} +let t1: T1 | T2 = { x: 1, y: "yy", z: true }; +Assert.equal(t1.x, 1); +Assert.equal(t1.y, "yy"); +Assert.equal(t1.z, true); +let t2: T1 | T2 = { x: "aa", y: "bb", z: "cc" }; +Assert.equal(t2.x, "aa"); +Assert.equal(t2.y, "bb"); +Assert.equal(t2.z, "cc"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b93a60ebd76f610cc839398152bdd5cf516477fe --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In several situations TypeScript infers types from context, + alleviating the need for the programmer to explicitly specify types that appear obvious. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var str = "xiao" +Assert.isString(str); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..efa91098654556a94263ded2c291a126ead05f47 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_2.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When inferring the type of a variable, property or function result from an expression, + the widened form of the source type is used as the inferred type of the target. + module: ESNext + isCurrent: true + ---*/ + + +import {Assert} from '../../../../suite/assert.js' + +var x = 10 +var y = 'a' +var z = x + y +Assert.isString(z); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1b02996d924d34839432f221036441813108489 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/types/widend_types/widend_types_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The widened form of a type is the type in which all occurrences of the Null and Undefined types have been replaced with the type any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var nu = null +Assert.equal(typeof nu, 'object') +var un = undefined +Assert.isUndefined(un) +var obj = { x: 0, y: null } +Assert.equal(typeof obj, 'object') +var arr = [null, undefined] +Assert.equal(typeof arr, 'object'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/tool/test_helper.py b/es2panda/test/ts_extra_tests/tool/test_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..cfc696877ce4ab21383a6c35ed209cf553543e6b --- /dev/null +++ b/es2panda/test/ts_extra_tests/tool/test_helper.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import re +import os + + +def read_declaration(path): + start_pattern = re.compile(r'^\/\*\*\-*') + end_pattern = re.compile(r'^\s*\-+\*\/') + context = "" + with open(path, 'r', encoding='utf-8', errors='ignore') as f: + declaration_begin = False + while True: + line = f.readline() + if not line: + break + if start_pattern.match(line): + declaration_begin = True + continue + if end_pattern.match(line): + declaration_begin = False + break + if declaration_begin: + context += line + return context + + +def is_root_dir(dir_path, file_or_dir, file_or_dir_results, limit_version): + rf = 'test_ts_cases' + root_folder = os.path.basename(dir_path) + if root_folder == rf: + # file_or_dir like: ['2.0', '2.1', '2.2', ... '4.9', 'spec'] + for f_item in file_or_dir: + if limit_version is None: + file_or_dir_results = file_or_dir + break + else: + limit_version = float(limit_version) + try: + f_num = float(f_item) + if f_num <= limit_version: + file_or_dir_results.append(f_item) + except Exception as e: + print(e) + continue + if limit_version is not None: + file_or_dir_results.append('spec') + + +def get_path_file(dir_path, all_file_path=None, is_root=False, limit_version=None): + if all_file_path is None: + all_file_path = [] + file_or_dir = os.listdir(dir_path) + file_or_dir_results = [] + if dir_path.endswith("test_ts_cases") or dir_path.endswith("test_ts_cases/"): + is_root = True + else: + is_root = False + if is_root: + is_root_dir(dir_path, file_or_dir, file_or_dir_results, limit_version) + else: + file_or_dir_results = file_or_dir + for file_dir in file_or_dir_results: + file_or_dir_path = os.path.join(dir_path, file_dir) + if '\\' in file_or_dir_path: + file_or_dir_path = file_or_dir_path.replace('\\', '/') + + if os.path.isdir(file_or_dir_path): + get_path_file(file_or_dir_path, all_file_path, False, None) + else: + all_file_path.append(file_or_dir_path) + + return all_file_path + + +def get_disable_list(file_path): + disable_list = [] + with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: + while True: + line = f.readline() + if not line: + break + disable_list.append(os.path.abspath(line.strip())) + return disable_list + + +def is_disable_case(file_path, disable_list): + if disable_list is None: + return False + if file_path in disable_list: + return True + for disable_path in disable_list: + if disable_path in file_path: + return True diff --git a/es2panda/test/ts_extra_tests/tool/testcfg.py b/es2panda/test/ts_extra_tests/tool/testcfg.py new file mode 100644 index 0000000000000000000000000000000000000000..0439de76791f0c6b5ee5f9571a31bead1e134db9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/tool/testcfg.py @@ -0,0 +1,277 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os +import re +import yaml +import platform +import subprocess +from tool.test_helper import read_declaration + +STRICT_OFF = ['--strict', 'false'] +STRICT_ON = ['--strict', 'true'] +MODULE = ['--module'] +DECORATOR = ['--experimentalDecorators'] +STRICTNULLCHECKS = ['--strictNullChecks'] + + +def get_error_message(strs, filename): + if len(re.findall(filename + r':(\d+)', strs)) > 0: + line_number = re.findall(filename + r':(\d+)', strs) + else: + line_number = 0 + err_message = strs + return err_message, line_number + + +class TestCase: + temp_path = "" + ld_library_path = "" + js_runtime_path = "" + es2abc = "" + tsc = "" + + def __init__(self, path): + self.path = path + self.target_js_path = "" + try: + data = yaml.safe_load(read_declaration(path)) + except: + data = {} + self.declaration = data + self.fail = False + self.is_test_case = False if data is None else True + self.detail_result = "" + self.err_line = 0 + self.abc_file_path = "" + self.abc_file_path_temp = "" + + def execute(self, ark_runtime=False): + if not self.is_test_case: + return + if ark_runtime: + with open(self.path, 'a') as file_added: + file_added.write('\rprint("TESTCASE SUCCESS");') + self.__test_es2abc() + if os.path.exists(self.abc_file_path): + os.remove(self.abc_file_path) + else: + self.__tsc_test() + + def is_negative(self): + if 'error' in self.declaration: + return True + return False + + def is_current(self): + if 'isCurrent' in self.declaration: + return True + return False + + def experimental_decorators(self): + if 'experimentalDecorators' in self.declaration: + return True + return False + + def null_checks(self): + if 'strictNullChecks' in self.declaration: + return True + return False + + def is_set_module(self): + if 'module' in self.declaration: + return True + return False + + def check_declaration(self): + if self.declaration == {}: + self.detail_result = "parse test case declaration failed, maybe bad format." + return False + if 'error' in self.declaration: + if self.declaration['error'] is None or 'code' not in self.declaration['error'] and 'type' not in \ + self.declaration['error']: + self.detail_result = "neither error code nor error type are defined in negative case." + return False + return True + + def __error_code(self): + if 'code' in self.declaration['error']: + return self.declaration['error']['code'] + return None + + def __error_type(self): + if 'type' in self.declaration['error']: + return self.declaration['error']['type'] + return None + + def __get_tsc_cmd(self): + if platform.system().lower() == 'windows': + cmd = ['cmd', '/c', 'tsc', '--target', 'es2020'] + else: + cmd = [TestCase.tsc, '--target', 'es2020'] + if self.__is_strict(): + cmd.extend(STRICT_ON) + else: + cmd.extend(STRICT_OFF) + if self.is_set_module(): + cmd.extend(MODULE) + cmd.append('es2020') + if self.experimental_decorators(): + cmd.extend(DECORATOR) + cmd.append('true') + if self.null_checks(): + cmd.extend(STRICTNULLCHECKS) + cmd.append('false') + if self.is_current(): + cmd.append(self.path) + cmd.append('--outDir') + cmd.append(TestCase.temp_path) + self.target_js_path = TestCase.temp_path + self.__get_js_basename() + return cmd + + def __get_node_cmd(self): + cmd = ['node'] + if self.is_current(): + cmd.append(self.target_js_path) + else: + cmd.append(TestCase.temp_path + self.__get_js_basename()) + return cmd + + # get es2abc --merge-abc + def __get_es2abc_cmd(self, file_path): + abc_file_path = ("%s.abc" % (os.path.splitext(file_path)[0])) + self.abc_file_path_temp = abc_file_path + cmd = [TestCase.es2abc + 'es2abc'] + cmd.extend(['--module', '--output', abc_file_path, file_path]) + return cmd + + # create abc files + def create_abc(self, filename): + process = subprocess.Popen(self.__get_es2abc_cmd(filename), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(timeout=5000) + return_code = process.returncode + if return_code != 0: + err_msg, line = get_error_message( + out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), filename) + self.detail_result = err_msg + self.err_line = line + self.fail = True + return + if "TESTCASE SUCCESS" not in out.decode("utf-8", errors="ignore"): + self.detail_result = "check stdout failed!" + self.fail = True + return + + # get es2abc file commands + def _get_ark_js_cmd(self): + os.environ.setdefault("LD_LIBRARY_PATH", TestCase.ld_library_path) + run_abc_cmd = [os.path.join(TestCase.js_runtime_path, 'ark_js_vm'), self.abc_file_path_temp] + return run_abc_cmd + pass + + def __get_js_basename(self): + sp = '/' + return "test_ts_cases" + sp + self.path.split(sp + "test_ts_cases" + sp)[1].replace('.ts', '.js') + + def __is_strict(self): + if 'strict' in self.declaration: + return bool(self.declaration['strict']) + return True + + def __tsc_test(self): + process = subprocess.Popen(self.__get_tsc_cmd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(timeout=5000) + return_code = process.returncode + if self.is_negative(): + if return_code == 0: + self.fail = True + self.detail_result = "No error found in negative case." + return + if self.__error_code() in out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"): + return + self.fail = True + self.detail_result = "Error code not as expected." + return + # positive case + if return_code != 0: + self.detail_result = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + self.fail = True + return + if self.is_current(): + with open(self.target_js_path, 'a') as fileAdded: + fileAdded.write('console.log("TESTCASE SUCCESS");') + # run node command + process = subprocess.Popen(self.__get_node_cmd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(timeout=5000) + return_code = process.returncode + if self.is_current(): + if os.path.exists(self.target_js_path): + os.remove(self.target_js_path) + if return_code != 0: + err_msg, line = get_error_message( + out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), self.__get_js_basename()) + self.detail_result = err_msg + self.err_line = line + self.fail = True + return + # check std out + if "TESTCASE SUCCESS" not in out.decode("utf-8", errors="ignore"): + self.detail_result = "check stdout failed!" + self.fail = True + return + + def __test_es2abc(self): + # compiler to abc + process = subprocess.Popen(self.__get_es2abc_cmd(self.path), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(timeout=5000) + return_code = process.returncode + if self.is_negative(): + if return_code == 0: + self.fail = True + return + if self.__error_type() in out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"): + return + self.fail = True + self.detail_result = "Error type not as expected." + return + # positive case + if return_code != 0: + self.detail_result = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + self.fail = True + return + # execute ark_js_vm + process = subprocess.Popen(self._get_ark_js_cmd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = process.communicate(timeout=5000) + return_code = process.returncode + if return_code != 0: + err_msg, line = get_error_message( + out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), + os.path.basename(self.abc_file_path)) + self.detail_result = err_msg + self.err_line = line + self.fail = True + return + # check std out + if "TESTCASE SUCCESS" not in out.decode("utf-8", errors="ignore"): + self.detail_result = "check stdout failed!" + self.fail = True + return