diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.ts new file mode 100644 index 0000000000000000000000000000000000000000..1023abc2b7e05c2850ca64bc4f0fe84ad7d88cb6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.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: > + An abstract class can declare abstract properties and/or accessors. Any sub class will need to declare the abstract + properties or be marked as abstract. Abstract properties cannot have an initializer. Abstract accessors cannot have bodies. +module: ESNext +isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +abstract class Skill { + abstract name: string; + abstract damage: number; + abstract get accessor(); + abstract set accessor(ok: string); +} +class Divied extends Skill { + name = "derived"; + damage = 128; + get accessor() { + return 'ok' + } + set accessor(ok: string) { + this.name = ok; + } +} +let d = new Divied(); +Assert.equal(d.name, "derived"); +Assert.equal(d.damage, 128); +Assert.equal(d.accessor, "ok"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6b6f3bf2cc427e1237c9988dfa2c3ac738291a2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_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: TypeScript 2.0 relaxes this constraint and allows duplicate identifiers across blocks, as long as they have identical types. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +interface Skill { + damage?: number; +} + +interface Skill { + skillName?: string; + damageType?: string; + damage?: number; +} + +let sa: Skill = { + damage: 1024, +}; + +let sb: Skill = { + skillName: "XOMissile", + damageType: "XO", + damage: 128, +}; + +Assert.equal(sa.damage, 1024); +Assert.equal(sb.damage, 128); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e5619f1eca1fa17f33d529cc6df2d6671a8b4c4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.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 checker analyses all possible flows of control in statements and expressions to produce the most specific type possible (the narrowed type) at any given location for a local variable or parameter that is declared to have a union type. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +let arg = Math.random() < 0.5 ? 5 : 'a'; +if (typeof arg === 'string') { + Assert.isString(arg); + arg = 3; + Assert.isNumber(arg); +} + +Assert.isNumber(arg); + + +function func(x: string | number) { + if (typeof x === "number") { + return "10"; + } + + return 10 +} + +Assert.isNumber(func("10")); +Assert.isString(func(10)); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.ts new file mode 100644 index 0000000000000000000000000000000000000000..99374a56ecc7ef0a424506186a6c747ae4406f1b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.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: > + Control flow based type analysis is particularly relevant in strictNullChecks mode because nullable types are represented using union types. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +function testFun(x: string | null): void { + if (x === null) { + return; + } + + Assert.isString(x); +} + +testFun(null); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7e7d38f213a981db1392feb97ebca0ca33f28f84 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.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: > + In strictNullChecks mode, control flow based type analysis includes definite assignment analysis for local variables of types that don’t permit the value undefined. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +function tFun(c: boolean): void { + let x: number; + if (c) { + x = 1; + Assert.equal(x, 1); + } + x = 2; + Assert.equal(x, 2); +} + +tFun(true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..30186c31c791d848bc9a870a465cf6805d0803d8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts @@ -0,0 +1,64 @@ +/* + * 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 now support checking “dotted names” consisting of a variable or parameter name followed one or more property accesses. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +type StrObj = + { + strobj?: { + str1?: string; + str2?: string; + }; + } + | undefined; + +function funSO(so?: StrObj): string | void { + if (so && so.strobj && so.strobj.str1) { + const x: string = so.strobj.str1; + Assert.isString(x); + return x; + } else if (so && so.strobj && so.strobj.str2) { + const x: string = so.strobj.str2; + Assert.isString(x); + return x; + } else { + if (so && so.strobj && so.strobj.str1 === undefined) { + return "so.strobj.str1 === undefined"; + } else if (so && so.strobj && so.strobj.str2 === undefined) { + return "so.strobj.str2 === undefined"; + } else if (so !== undefined && so.strobj === undefined) { + return "so.strobj === undefined"; + } else if (so === undefined) { + return "so === undefined"; + } + } +} + +let so: StrObj = undefined; +Assert.equal(funSO(so), "so === undefined"); +so = {}; +Assert.equal(funSO(so), "so.strobj === undefined"); +so = { strobj: {} }; +Assert.equal(funSO(so), "so.strobj.str1 === undefined"); +so = { strobj: { str1: "str1" } }; +Assert.equal(funSO(so), "str1"); +so = { strobj: {str2: "str2"} }; +Assert.equal(funSO(so), "str2"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4881985ea697e2b62fe778b5bbd35333a009f59f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_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: Expression operators permit operand types to include null and/or undefined but always produce values of non-null and non-undefined types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +let x: number | undefined; +let y: string | null; +let method: string | number | undefined | null; + +x = undefined; +y = 'a'; +method = null +method = x + y + method; +Assert.isString(method); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a5b375bec60bd538966ca1935edef2ca53602b9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/expression_operators/expression_operators_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 && operator adds null and/or undefined to the type of the right operand depending on which are present in the type of the left operand, and the || operator removes both null and undefined from the type of the left operand in the resulting union type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +type o = { + num: 10 +} +type T = o | undefined; +let x: T; + +let y = x && x.num; +y = 10; +Assert.isNumber(y); +y = undefined; +Assert.isUndefined(y); + +let z = x || { num: 5 }; +Assert.isObject(z); + +interface I{ + str: string; +} +type t = I | undefined; +let a: t; + +let b = a && a.str; +b = ' '; +Assert.isString(b); +b = undefined; +Assert.isUndefined(b); + +let c = a || { str: 's' }; +Assert.isObject(c); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/implicit_index_signatures/implicit_index_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/implicit_index_signatures/implicit_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..e49b071acc8381041640f2a5dfe2095167d9da65 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/implicit_index_signatures/implicit_index_signatures.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: > + An object literal type is now assignable to a type with an index signature if all known properties in the + object literal are assignable to that index signature. This makes it possible to pass a variable that was initialized with an object literal as parameter to a function that expects a map or dictionary: + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func(str: string, arg: { [x: string]: string }) { + return JSON.stringify(arg); +} +const arg = { + "Color": "Red", +}; +let s1: string = func("", { + "Color": "red", +}); +Assert.equal(s1, '{"Color":"red"}'); + +let s2: string = func("", arg); +Assert.equal(s2, '{"Color":"Red"}'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5716d30ff454c547221bf30a0a8462ccb3bf10c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts @@ -0,0 +1,26 @@ +/* + * 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: With TypeScript 2.0, the compiler will look up definition of "moduleA.js" in ./moduleA.ts or ./moduleA.d.t. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" +import { HelloWorld } from "./test_js_extension.js" + +let world = new HelloWorld("HelloWorld.txt"); +Assert.equal(world.txt, "HelloWorld.txt"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts new file mode 100644 index 0000000000000000000000000000000000000000..f9b155f734579df7919a4cea45738325cf345bea --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts @@ -0,0 +1,22 @@ +/* + * 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 HelloWorld { + txt: string; + constructor(name: string) { + this.txt = name; + } +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/never_type/never_type.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/never_type/never_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..28d93a7c671573f8f9cc99bd64d095b92a64a823 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/never_type/never_type.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: > + never type + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function testError():never { + throw new Error("testError"); +} + +try { + testError(); +} catch (e:any) { + Assert.equal(e.message, "testError"); +} + +function test():never { + return "ok" as never +} + +Assert.equal(typeof test(), "string"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_and_non_undefined/non_null_and_non_undefined.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_and_non_undefined/non_null_and_non_undefined.ts new file mode 100644 index 0000000000000000000000000000000000000000..eebda7ab589f168811617255acdd88d6dd3c44c0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_and_non_undefined/non_null_and_non_undefined.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: > + Non-null and non-undefined type guards + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function testNullAndUndefined(x: number): string { + return x.toString(); +} + +let x: number | null | undefined; +x = 1; + +Assert.equal(testNullAndUndefined(x), '1'); + +let y: number | null | undefined; + +let ad = y && testNullAndUndefined(y) ; +ad = "1" +Assert.equal(ad, "1"); +ad = undefined +Assert.equal(ad, undefined); +ad = null; +Assert.equal(ad, null); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_assertion_operator/non_null_assertion_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_assertion_operator/non_null_assertion_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..45bd994f48f471d1ca1561c8baf13a58ef399d7b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/non_null_assertion_operator/non_null_assertion_operator.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: > + A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +function testStr(x?: string, y?: number | string): string { + let z: number | string = x! + y!; + return z; +} +Assert.equal(testStr("A"), "Aundefined"); +Assert.equal(testStr("B", 5), "B5"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/null_and_undefined_aware_types/null_and_undefined_aware_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/null_and_undefined_aware_types/null_and_undefined_aware_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..9291b75e83b5d9bbe308cb74ca50d6d6e542ba5a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/null_and_undefined_aware_types/null_and_undefined_aware_types.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: > + Null- and undefined-aware types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +let a: number; +let b: number | undefined; +let c: number | null | undefined; + +a = 0; +Assert.equal(a,0); +b = 0; +Assert.equal(b,0); +b = undefined; +Assert.equal(b,undefined); +c = 0; +Assert.equal(c,0); +c = undefined; +Assert.equal(c,undefined); +c = null; +Assert.equal(c,null); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_class_properties/optional_class_properties.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_class_properties/optional_class_properties.ts new file mode 100644 index 0000000000000000000000000000000000000000..d8e5a35e47f9b6cae0ad5b0aca9763ac10b983e1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_class_properties/optional_class_properties.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: Optional properties and methods can now be declared in classes, similar to what is already permitted in interfaces. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class Demo { + a: number = 0; + b?: number; + func() { + return this.a; + } + + get?(): number; + handle?() { + return this.b; + } +} +let c = new Demo(); +c.a = 1024; +c.b = 1408; +Assert.equal(c.func(), 1024); + +if (c.handle) { + Assert.equal(c.handle(), 1408); +} + +c.get = () => { + if (c.b !== undefined) { + return c.a + c.b; + } else { + return c.a; + } +} +Assert.equal(c.get(), 2432); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_parameters_and_properties/optional_parameters_and_properties.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_parameters_and_properties/optional_parameters_and_properties.ts new file mode 100644 index 0000000000000000000000000000000000000000..8aa15f10aa4fda0e36dec0c620f5beb4e1599f2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/optional_parameters_and_properties/optional_parameters_and_properties.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: Optional parameters and properties automatically have undefined added to their types, even when their type annotations don’t specifically include undefined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js"; + +function printPoint(a: number, b: number, c?: number): string { + let obj: { a: number, b: number, c?: number }; + if (c !== undefined) { + obj = { a, b, c }; + Assert.isNumber(c); + } else { + obj = { a, b }; + Assert.isUndefined(c); + } + return JSON.stringify(obj); +} +Assert.equal(printPoint(1, 3, 5), '{"a":1,"b":3,"c":5}'); +Assert.equal(printPoint(1, 1), '{"a":1,"b":1}'); + +interface I{ + num: number; + str?: string; +} +let i1: I = { + num: 5 +} +Assert.isUndefined(i1.str); +let i2: I = { + num: 5, + str: 'a' +} +Assert.isString(i2.str); + +class C{ + num: number; + str?: string; + constructor(num: number, str?: string) { + this.num = num; + this.str = str; + } +} +let c1 = new C(10); +Assert.isUndefined(c1.str); +let c2 = new C(10, 'a'); +Assert.isString(c2.str); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/private_and_protected_constructor/private_and_protected_constructor.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/private_and_protected_constructor/private_and_protected_constructor.ts new file mode 100644 index 0000000000000000000000000000000000000000..dda0fb44a33989ef0374dc2e502dd971a8060a45 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/private_and_protected_constructor/private_and_protected_constructor.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 class constructor may be marked private or protected. A class with private constructor cannot be instantiated outside the class body, and cannot be extended. A class with protected constructor cannot be instantiated outside the class body, but can be extended. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class Box { + s!:string; + private static subBox: Box; + private constructor() { } + static getSubBos(s:string) { + if (!Box.subBox) { + Box.subBox = new Box(); + Box.subBox.s = s; + } + return Box.subBox; + } +} +let v: Box = Box.getSubBos("s"); +Assert.equal(v.s, "s"); + +class myBox{ + arg: string; + protected constructor(arg: string) { + this.arg = arg; + } +} +class myLittleBox extends myBox{ + constructor(arg: string) { + super(arg); + } +} +let tool = new myLittleBox('hammer'); +Assert.equal(tool.arg, 'hammer'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..570e4f955e83c0cd7d9347dd7d4692cad8cbe3c3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.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: > + A property or index signature can now be declared with the readonly modifier is considered read-only. + Read-only properties may have initializers and may be assigned to in constructors within the same class declaration, but otherwise assignments to read-only properties are disallowed + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface Demo { + readonly x: number; + readonly y: string; + readonly z: boolean; +} +let d2 = { x: 1, y: "aa", z: true }; +let d3: Demo = d2; +Assert.equal(JSON.stringify(d3), '{"x":1,"y":"aa","z":true}'); +d2.x = 5; +Assert.equal(d3.x, 5) + +let x: Array = [0, 1, 2]; +let y: ReadonlyArray = x; +Assert.equal(JSON.stringify(y), '[0,1,2]'); + +class Demo2 implements Demo { + readonly x: number; + readonly y: string; + readonly z: boolean; + constructor(z: boolean) { + this.x = 1; + this.y = "demo"; + this.z = z; + } +} +let d4: Demo2 = new Demo2(true); +Assert.equal(d4.x, 1); +Assert.equal(d4.y, "demo"); +Assert.equal(d4.z, true); + +interface Demo3 { + [key: string]: number; + x: number; +} +let d5: Demo3 = { + x: 1, + y: 2 +}; +Assert.equal(d5['x'], 1); +Assert.equal(d5['y'], 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..14c73669a0daf76f739c789ffe4b268e14ef0941 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_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: > + If you don’t want to take the time to write out declarations before using a new module, + you can now just use a shorthand declaration to get started quickly. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" +import hwexpNumAdd15, { y } from "./test_ts.js" + +Assert.equal(hwexpNumAdd15(y), 25); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/test_ts.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/test_ts.ts new file mode 100644 index 0000000000000000000000000000000000000000..13cc01685187887ca623236eb6232c3b5d3046bd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/shorthand_ambient_module_declarations/test_ts.ts @@ -0,0 +1,21 @@ +/* + * 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 default function hwexpNumAdd15(a: number) { + return a + 15; +} + +export const y = 10; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea3bcc250175105e648ce6c36b5840d07a67172f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.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: You can provide an explicit this parameter. this parameters are fake parameters that come first in the parameter list of a function. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +function fun_this(this: any) { + Assert.equal(this, undefined); +} + +fun_this(); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9348bd0d1fdfaba063ee255c4b3b43cf2885eeb0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_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: > + this parameters in callbacks.Libraries can also use this parameters to declare how callbacks will be invoked. + this: void means that addClickListener expects onclick to be a function that does not require a this type. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +interface Web { + Click(onclick: (this: void, e: Event) => void): void; +} + +class NetLink { + info: string; + constructor(info: string) { + this.info = info; + } + onClickGood(this: void, e: Event) { + } +} + +let web: Web = { + Click(Onclick: (this: void, e: Event) => void) { + Assert.equal(typeof this, "object"); + } +}; +let onAir = new NetLink("OnAir"); +web.Click(onAir.onClickGood); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/tagged_union_types/tagged_union_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/tagged_union_types/tagged_union_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f736a3b46971dc21f9c59bb9bb85ac770d563011 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/tagged_union_types/tagged_union_types_1.ts @@ -0,0 +1,67 @@ +/* + * 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 TS compiler now support type guards that narrow union types based on tests of a discriminant property and furthermore extend that capability to switch statements. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +type Color = { + name: "Color"; + rgb: [number, number, number]; +} + +type Point = { + name: "Point"; + point: [number, number]; +} + + + +type ColorPoint = Color | Point +let un:ColorPoint[] = [{name:"Color", rgb:[0,0,0]},{name:"Point", point:[1,1]}]; +let count:number = 0; +function test(s: ColorPoint[]) { + for (const unElement of s) { + switch (unElement.name) { + case "Color": + count++ + break + case "Point": + count++ + break + } + } + return count; +} + +function test1(s: ColorPoint) { + if (s.name === "Color") { + return s; + } + return s; +} + +let point: Point = { + name: "Point", + point: [0, 0] +}; + + +Assert.equal(JSON.stringify(test(un)), 2); +Assert.equal(test1(point).name, 'Point'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_base_url.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_base_url.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d0e09479a43ea0eca2ae3082941b8e1e3084f61 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_base_url.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +function add(a: number, b: number): number { + return a + b; +} +let flag: string = "baseurl"; +export { add as ADD, flag }; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_virtual_directories_with_rootDirs/view2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_virtual_directories_with_rootDirs/view2.ts new file mode 100644 index 0000000000000000000000000000000000000000..58d9a6f69e993091f72f243032bbf94aea350cfe --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/test_virtual_directories_with_rootDirs/view2.ts @@ -0,0 +1,22 @@ +/* + * 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 enum Directions { + Up, + Down, + Left, + Right +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.0/trailing_commas_in_parameter_and_argument/trailing_commas_in_parameter_and_argument.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/trailing_commas_in_parameter_and_argument/trailing_commas_in_parameter_and_argument.ts new file mode 100644 index 0000000000000000000000000000000000000000..28859f9abb05b0f53644f6f0e696b50f25e49a9b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.0/trailing_commas_in_parameter_and_argument/trailing_commas_in_parameter_and_argument.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: Trailing commas in function parameter and argument lists + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function point(x:number, y:number,) { + return x+y +} + +Assert.equal(3,point(1,2 )) +Assert.equal(3,point(1,2, )) diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d276b7f620332b04f6001928cabfe02d025864cb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.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: > + The type inferred for a const variable or readonly property without a type annotation is the type of the literal initializer. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +const a = 1; +let aa: typeof a = 1; +Assert.equal(typeof aa , "number"); +const b = false; +let bb: typeof b = false; +Assert.equal(typeof bb , "boolean"); +const c = "hello"; +let cc: typeof c = "hello"; +Assert.equal(typeof cc , "string"); + + +class Test { + static readonly age = 12; + static readonly isJob = false; + static readonly nickName = "wangcai"; +} + +let age: typeof Test.age = 12; +Assert.equal(typeof age, "number"); +let isJob: typeof Test.isJob = false; +Assert.equal(typeof isJob, "boolean"); +let nickName: typeof Test.nickName = "wangcai"; +Assert.equal(typeof nickName, "string"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..497649f49cd05949be65308a406789af8cb7e525 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.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: > + The type inferred for a let variable, var variable, parameter, or non-readonly property with an initializer + and no type annotation is the widened literal type of the initializer. + where the widened type for a string literal type is string, number for numeric literal types, boolean for true or false + and the containing enum for enum literal types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let a = 1; +let newA: typeof a = 12; +Assert.isNumber(newA); + +let b = 1; +let newB: typeof b = 12; +Assert.isNumber(newB); + +function test(a:boolean = true) { + return a; +} +let newT: ReturnType = false; +Assert.isBoolean(newT); + +class Test { + static job = "coder"; +} + +let newC: typeof Test.job = "driver"; +Assert.isString(newC); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..efe57c6d4d16cbd54c5d9c9712e6eefe8e47f194 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.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: Literal type widening can be controlled through explicit type annotations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +const newString = "hello"; + +let newV = newString; +newV = "world"; +const newS2: "hello" = "hello"; +let newV2 = newS2; +Assert.equal(newV2, "hello"); + +const newN = 1; +let n4 = newN; +n4 = 10; +Assert.equal(n4, 10); +const n5: 1 = 1; +let n6 = n5; +n6 = 1; +Assert.equal(n6, 1); + +const newB = false; +let b8 = newB; +b8 = true; +Assert.equal(b8, true); +const b9: false = false; +let b10 = b9; +b10 = false; +Assert.equal(b10, false); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/downlevel_async_functions/downlevel_async_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/downlevel_async_functions/downlevel_async_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2987c4035422febe5b68e60b47aabdd92a90ff35 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/downlevel_async_functions/downlevel_async_functions_1.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: > + This feature was supported before TypeScript 2.1, + but only when targeting ES6/ES2015. + TypeScript 2.1 brings the capability to ES3 and ES5 run-times. + options: + lib:dom,es5,es2015.promise + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function dTime(milli: number) { + return new Promise((resolve) => { + setTimeout(resolve, milli); + }); +} +async function showMsg(a: number, b: string) { + Assert.equal(a, 12); + for (let i = 0; i < 5; i++) { + await dTime(300); + Assert.equal(b, "New"); + } + Assert.equal(b, "New"); +} +showMsg(12, "New"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c5a618c9a7e5670640a08a9e374e2bdafef70a73 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_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: > + TypeScript2.1 will infer types based on what you end up assigning later on. + This is only enabled if noImplicitAny is set. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let win; +win = (a: number) => a + 1; + +let fail = win; +Assert.equal(fail(12), 13); + +win = "abc"; +Assert.isString(win); +Assert.equal(win.toUpperCase(), "ABC"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ee331afe7577167aad602240f3bae5ad1c72b4f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/improved_any_inference/improved_any_inference_2.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 variable declared with no type annotation and an initial value of [] is considered an implicit any[] variable. + However, each subsequent x.push(value), x.unshift(value) or x[n] = value operation evolves the type of the variable + in accordance with what elements are added to it. + This is only enabled if noImplicitAny is set. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function funcT() { + let arr = []; + arr[0] = 0; + arr.push("true"); + arr.unshift("abc"); + return arr; +} + +type newF = ReturnType; + +let funC: newF = ["hello", 12]; +Assert.equal(JSON.stringify(funC), '["hello",12]'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..efbc1da5aae09201d9337295f00898b963194686 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts @@ -0,0 +1,64 @@ +/* + * 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 indexed type query keyof T yields the type of permitted property names for T. + A keyof T type is considered a subtype of string. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class TestHuman { + name: string; + age: number; + private job: string; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} +type TestName = Pick; +let nn: TestName = { + name: "ayw", +}; +Assert.notEqual(nn.name, undefined); +Assert.equal(nn.name, "ayw"); + +type TestAge = Pick; +let aa: TestAge = { + age: 20, +}; +Assert.notEqual(aa.age, undefined); +Assert.equal(aa.age, 20); + +type Test = Pick; +let cc: Test = { + name: "cool", + age: 15, +}; +Assert.notEqual(cc.name, undefined); +Assert.notEqual(cc.age, undefined); + +Assert.equal(cc.name, "cool"); +Assert.equal(cc.age, 15); + +type TestString = keyof { [x: string]: number }; + +let str: TestString = "abc"; +Assert.isString(str); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..47410ddcb11652f1a4868b5f12859d6f49b8f747 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/keyof_and_lookup_types/keyof_and_lookup_types_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: indexed access types, also called lookup types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class TestHuman { + name: string; + age: number; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + } +} + +type TestName = TestHuman["name"]; +let test1: TestName = ""; +Assert.equal(typeof test1, "string"); +Assert.notEqual(typeof test1, "number"); +Assert.notEqual(typeof test1, "boolean"); + +type TestAge = TestHuman["age"]; +let test2: TestAge = 0; +Assert.equal(typeof test2, "number"); +Assert.notEqual(typeof test2, "string"); +Assert.notEqual(typeof test2, "boolean"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1556bb11b53e777301400879b6a6d75cfb9f1b96 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_1.ts @@ -0,0 +1,64 @@ +/* + * 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: > + Partial + Constructs a type with all properties of Type set to optional. + This utility will return a type that represents all subsets of a given type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface HumanTest { + name: string; + age: number; + destination: string; +} +type PersonType = Partial; + +let pt1: HumanTest = { + name: "caihua", + age: 20, + destination: "earth", +}; + +let pt2: PersonType; +pt2 = {}; +Assert.equal(pt2.name, undefined); +Assert.equal(pt2.age, undefined); +Assert.equal(pt2.destination, undefined); +pt2 = { name: "caihua" }; +Assert.equal(pt2.name, "caihua"); +Assert.equal(pt2.age, undefined); +Assert.equal(pt2.destination, undefined); +pt2 = { age: 20 }; +Assert.equal(pt2.name, undefined); +Assert.equal(pt2.age, 20); +Assert.equal(pt2.destination, undefined); +pt2 = { destination: "earth" }; +Assert.equal(pt2.name, undefined); +Assert.equal(pt2.age, undefined); +Assert.equal(pt2.destination, "earth"); +pt2 = { name: "caihua", age: 20 }; +Assert.equal(pt2.name, "caihua"); +Assert.equal(pt2.age, 20); +Assert.equal(pt2.destination, undefined); +pt2 = { name: "lwx", age: 27, destination: "nanj" }; +Assert.equal(pt2.name, "lwx"); +Assert.equal(pt2.age, 27); +Assert.equal(pt2.destination, "nanj"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb4e1c8314fe74bc46255f6d158cd4275ecb51bb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_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: > + Readonly + Constructs a type with all properties of Type set to readonly, + meaning the properties of the constructed type cannot be reassigned. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface HumanTest { + name: string; + age: number; + destination: string; +} +type PersonType = Readonly; + +let pt1: HumanTest = { + name: "caihua", + age: 20, + destination: "earth", +}; +pt1.name = "caihua1"; +Assert.notEqual(pt1.name, "caihua"); +pt1.age = 15; +Assert.notEqual(pt1.age, 20); +pt1.destination = "Mars"; +Assert.notEqual(pt1.destination, "earth"); +let pt2: PersonType = { + name: "caihua", + age: 20, + destination: "earth", +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea6ef38b4ca419846b4197402e3058caa169cd63 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_3.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: > + Record + Constructs an object type whose property keys are Keys and whose property values are Type. + This utility can be used to map the properties of a type to another type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface CatInfoTest { + age: number; + breed: string; +} + +type CatNameType = "miffy" | "boris" | "mordred"; + +const cats: Record = { + miffy: { age: 10, breed: "Persian" }, + boris: { age: 5, breed: "Maine Coon" }, + mordred: { age: 16, breed: "British Shorthair" }, +}; +Assert.equal(cats.miffy.age, 10); +Assert.equal(cats.boris.age, 5); +Assert.equal(cats.mordred.age, 16); +Assert.equal(cats.miffy.breed, "Persian"); +Assert.equal(cats.boris.breed, "Maine Coon"); +Assert.equal(cats.mordred.breed, "British Shorthair"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..af235985d89b133050f519632df0b5ea872fb714 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/mapped_types/mapped_types_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: > + Pick + Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface TodoInter { + title: string; + description: string; + completed: boolean; +} + +type TodoTitle = Pick; +type TodoTitleOrCompleted = Pick; +type TodoAll = Pick; + +const title: TodoTitle = { + title: "Clean room", +}; +Assert.equal("title" in title, true); +Assert.equal("description" in title, false); +Assert.equal("completed" in title, false); + +const titleAndCompleted: TodoTitleOrCompleted = { + title: "Clean room", + completed: false, +}; +Assert.equal("title" in titleAndCompleted, true); +Assert.equal("completed" in titleAndCompleted, true); +Assert.equal("description" in titleAndCompleted, false); + +const testAll: TodoAll = { + title: "Clean room", + description: "clean the whole room", + completed: false, +}; +Assert.equal("title" in testAll, true); +Assert.equal("description" in testAll, true); +Assert.equal("completed" in testAll, true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..33bbe63b23cdebdfa4958d0e6d959ecc827e9dcc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_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: spreading an object can be handy to get a shallow copy. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let humen = { + name: "caihua", + age: 20, +}; +let job = { job: "stu" }; +let data = { + location: "mars", + pets: { + type: "dog", + name: "ahuang", + }, +}; + +let combine = { ...humen, ...job, ...data }; + +Assert.equal(combine.name, humen.name); +Assert.equal(combine.age, humen.age); +Assert.equal(combine.job, job.job); +Assert.equal(combine.location, data.location); +Assert.equal(combine.pets.name, data.pets.name); +Assert.equal(combine.pets.type, data.pets.type); +Assert.equal(combine.name == humen.name, true); +Assert.equal(combine.age == humen.age, true); +Assert.equal(combine.job == job.job, true); +Assert.equal(combine.location == data.location, true); +Assert.equal(combine.pets == data.pets, true); +Assert.equal(combine.pets.type == data.pets.type, true); +Assert.equal(combine.pets.name == data.pets.name, true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b1976e7645316e8fa4602d9ede5b45ae7367ea1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_2.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: > + override existing properties and add new ones + The order of specifying spread operations determines what properties end up in the resulting object; + properties in later spreads "win out" over previously created properties. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let humen = { + name: "caihua", + age: 20, +}; + +let combine = { ...humen, name: "huahua", job: "teacher" }; + +Assert.equal(combine.age, humen.age); +Assert.equal(combine.age == humen.age, true); +Assert.equal(combine.name != humen.name, true); + +Assert.equal(combine.job != undefined, true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..83fadffad51d5612b5681e8a0b1a530145059d6e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/object_spread_and_rest/object_spread_and_rest_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: > + override existing properties and add new ones + The order of specifying spread operations determines what properties end up in the resulting object; + properties in later spreads "win out" over previously created properties. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let humen = { + name: "caihua", + age: 20, +}; + +let combine = { ...humen, name: "huahua", job: "teacher" }; + +let { job, ...restProperties } = combine; +Assert.equal(job, combine.job); +Assert.equal(restProperties.age, combine.age); +Assert.equal(restProperties.name, combine.name); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/record/record_pick.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/record/record_pick.ts new file mode 100644 index 0000000000000000000000000000000000000000..bfe31d37c06a33154c25ffefcf60410d998884d2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/record/record_pick.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: > + Record pick + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function test(o: Record, testObj: (s: B) => C): Record { + + for (let oElement in o) { + // @ts-ignore + o[oElement] = testObj(o[oElement]); + } + // @ts-ignore + return o; +} + +let testO = { ok: "1", demo: "2" }; +let ret = test(testO, str => str.length); +Assert.isNumber(ret.ok) + +let person:{name?:string, age?:string} = {}; +function test1(o: T, ...ks: K[]): Pick{ + for (let oElement of ks) { + // @ts-ignore + o[oElement] = oElement; + } + return o; +} +test1(person, "name", "age"); +Assert.equal(person.name, 'name'); +Assert.equal(person.age, 'age'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1cbabba76773a86566853b525975d3c49e92ff2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.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: > + In ES2015, constructors which return an object implicitly substitute the value of this for any callers of super(). + it is necessary to capture any potential return value of super() and replace it with this. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class Test { + one: number; + constructor(one: number) { + this.one = one; + return { + one: 1, + }; + } +} +class Test_sub extends Test { + constructor(x: number) { + super(x); + } +} +let newC = new Test_sub(12); +Assert.equal(newC.one, 1); +Assert.notEqual(newC.one, 12); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..a6dbefe1f5cbcf5ec69663dbe42e1b31d1b204c3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.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: > + types with a string index signature can be indexed using the "[]" notation, but were not allowed to use the ".". Starting with TypeScript 2.2 using either should be allowed. + this only apply to types with an explicit string index signature. It is still an error to access unknown properties on a type using "." notation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +type myType = { [arg: string]: string }; +let my_t: myType = { + 'color': 'blue' +} + +Assert.equal(my_t['color'], 'blue'); +Assert.equal(my_t.color, 'blue'); + +class C{ + Arg: { [arg: string]: string }; + constructor(Arg: { [arg: string]: string }) { + this.Arg = Arg; + } +} +let c = new C({ 'color': 'black' }); +Assert.equal(c.Arg['color'], 'black'); +Assert.equal(c.Arg.color, 'black'); + +interface StringArrIndex { + [x: string]: T; +} +var strArr: StringArrIndex = {}; +strArr["ATP"] = 0x00; +strArr.NARC = 0x01; +Assert.equal(strArr.ATP, 0x00); +Assert.equal(strArr["NARC"], 0x01); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1961d022580dd312cf4d6946e075e332602997f4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_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: > + typeScript did not have a type that represents the non-primitive type, i.e. any thing that is not number, string, + boolean, symbol, null, or undefined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +var obj1 = { num: 0, str: "string", bool: false, null: null, undefined: undefined }; +Assert.isNumber(obj1.num); +Assert.isString(obj1.str); +Assert.isBoolean(obj1.bool); +Assert.equal(obj1.null, null); +Assert.equal(obj1.undefined, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..63fe0025e20e51160967cc59356dffdf64aa9912 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_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: > + typeScript did not have a type that represents the non-primitive type, i.e. any thing that is not number, string, + boolean, symbol, null, or undefined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let obj2 = { num: 1 }; +let obj3 = { str: "OBJ" }; +let obj4 = { num: 1 }; +Assert.equal(typeof obj2, 'object'); +Assert.equal(typeof obj3, 'object'); +Assert.isTrue(typeof obj2 === typeof obj3); +Assert.isFalse(obj2 === obj4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..6007f1806fcc0c499259e144eefc0933e920e16d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/object_type/object_type_3.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: > + Create an object type variable + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let obj5 = new Object({ Damage: 1024, DamageType: 'XO' }); +Assert.equal(JSON.stringify(obj5), '{"Damage":1024,"DamageType":"XO"}'); + +type ColorOBJ = { Color: [number, number, number], ColorName: string }; +let obj6: ColorOBJ = { Color: [255, 0, 0], ColorName: 'Red' }; +Assert.equal(JSON.stringify(obj6), '{"Color":[255,0,0],"ColorName":"Red"}'); + +interface Weapon { + Damage: number; + DamageType: string; +} +let obj7: Weapon = { Damage: 333, DamageType: 'EXP' }; +Assert.equal(JSON.stringify(obj7), '{"Damage":333,"DamageType":"EXP"}'); + +let obj8: object = { A: 1 }; +Assert.equal(JSON.stringify(obj8), '{"A":1}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1a2eea5233faf9685ef9c3d5dfeef1592bdd088 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts @@ -0,0 +1,75 @@ +/* + * 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 2.2 adds support for the ECMAScript 2015 mixin class pattern as well as rules for combining mixin construct signatures with regular construct signatures in intersection types. + a mixin constructor type refers to a type that has a single construct signature with a single rest argument of type any[] and an object-like return type. + a mixin class is a class declaration or expression that extends an expression of a type parameter type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +class PointXY { + public x: number; + public y: number; + constructor(x: number = 0, y: number = 0) { + this.x = x; + this.y = y; + } + toString() { + return '(' + this.x + ', ' + this.y + ')'; + } +} +type MIXType = new (...members: any[]) => T; +function mixC>(BC: T) { + return class extends BC { + constructor(...members: any[]) { + super(...members); + } + public pname: string = ""; + setPname(pname: string) { + this.pname = pname; + } + getPname() { + return this.pname; + } + }; +} +const PointMix = mixC(PointXY); +var a = new PointMix(4, 4); +var b = new PointMix(); +a.setPname("A"); +b.setPname("B"); +Assert.equal(a.toString(), "(4, 4)"); +Assert.equal(b.getPname(), "B"); + +class PointMixCopy extends mixC(PointXY) { + public occupied: boolean = false; + setOccupied(occupied: boolean) { + this.occupied = occupied; + } + getOccupied() { + return this.occupied; + } +} +var c = new PointMixCopy(5, 5); +c.setPname("C"); +c.setOccupied(true); +Assert.equal(c.toString(), "(5, 5)"); +Assert.equal(c.getPname(), "C"); +Assert.equal(c.getOccupied(), true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a83122940493adf16ff8b7b397c6810834951e9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_mix_in_classes/support_for_mix_in_classes_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: > + mixin classes can constrain the types of classes they can mix into by specifying a construct signature return type in the constraint for the type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class Color { + public Red: number; + public Green: number; + public Blue: number; + constructor(red: number = 0x00, green: number = 0x00, blue: number = 0x00) { + this.Red = red; + this.Green = green; + this.Blue = blue; + } + toString() { + return 'Color(' + this.Red + ', ' + this.Green + ', ' + this.Blue + ')'; + } +} +type MIXTypeCopy = new (...members: any[]) => T; +function ColorClassMix>(BC: T) { + return class extends BC { + getColors(): [number, number, number] { + return [this.Red, this.Green, this.Blue]; + } + } +} + +const ColorMix = ColorClassMix(Color); +var color = new ColorMix(255, 255, 255); +Assert.equal(color.toString(), "Color(255, 255, 255)"); +Assert.equal(color.getColors()[0], 255); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d348065eaac5038481335920f7ea8d241b50997 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_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: > + the new.target meta-property is new syntax introduced in ES2015. When an instance of a constructor is created via new, the value of new.target is set to be a reference to the constructor function initially used to allocate the instance. + if a function is called rather than constructed via new, new.target is set to undefined. + options: + lib: es2015 + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class A { + public cname: string; + constructor() { + this.cname = new.target.name; + } +} + +class B extends A { constructor() { super(); } } + +var na = new A(); +Assert.equal(na.cname, "A"); +var nb = new B(); +Assert.equal(nb.cname, "B"); + +class C { + public data: any; + constructor() { + this.data = new.target; + } +} +class D extends C { constructor() { super(); } } + +var nc = new C(); +Assert.equal(nc.data.name, "C"); +var nd = new D(); +Assert.equal(nd.data.name, "D"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..bce50c300310620020eb6ad1c2e4e48f1b0ad3f0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.2/support_for_new.target/support_for_new.target_2.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 new.target meta-property is new syntax introduced in ES2015. When an instance of a constructor is created via new, the value of new.target is set to be a reference to the constructor function initially used to allocate the instance. + if a function is called rather than constructed via new, new.target is set to undefined. + options: + lib: es2015 + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +var gstr: string = ""; +function newTarget(): void { + if (new.target === undefined) { + gstr = "The function is called without \"new\""; + } else { + gstr = "The function is called with \"new\""; + } +} +newTarget(); +var ntf1: string = gstr; +Assert.equal(ntf1, "The function is called without \"new\""); + +new (newTarget as any)(); +var ntf2: string = gstr; +Assert.equal(ntf2, "The function is called with \"new\""); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_generators/async_generators.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_generators/async_generators.ts new file mode 100644 index 0000000000000000000000000000000000000000..95369ceef3c0ed5b2842ad1ebe2d657b726d04c3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_generators/async_generators.ts @@ -0,0 +1,71 @@ +/* + * 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 Async Iteration proposal introduces "Async Generators", which are async functions that also can be used to yield partial computation results. Async Generators can also delegate calls via yield* to either an iterable or async iterable. + as with Generators, Async Generators can only be function declarations, function expressions, or methods of classes or object literals. Arrow functions cannot be Async Generators. Async Generators require a valid, global Promise implementation (either native or an ES2015-compatible polyfill), in addition to a valid Symbol.asyncIterator reference. + options: + lib: es2018 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +async function* fun() { + let i = 0; + let c = 0; + yield* [0, 0, 0, 0]; + while (true) { + if (i % 3 == 0) { + yield c -= 1; + } else { + yield c += 1; + } + i++; + if (i > 30) { + break; + } + } +} +function newArray() { + let arr: number[] = [0, 0, 0, 0]; + let i = 0; + let c = 0; + while (true) { + if (i % 3 == 0) { + c -= 1; + arr[i + 4] = c; + } else { + c += 1; + arr[i + 4] = c; + } + i++; + if (i > 30) { + break; + } + } + return arr; +} +let arr = fun(); +async function showAsyncGenerator(arr: AsyncGenerator) { + let i = 0; + for await (let x of arr) { + Assert.equal(x as number, arr2[i++]); + } +} +let arr2 = newArray(); +showAsyncGenerator(arr); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..53ba33954d8d84471f1d56b8b10c10b4998f6c9f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_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: > + the Async Iteration introduces an AsyncIterator, which is similar to Iterator. The difference lies in the fact that the next, return, and throw methods of an AsyncIterator return a Promise for the iteration result, rather than the result itself. + this allows the caller to enlist in an asynchronous notification for the time at which the AsyncIterator has advanced to the point of yielding a value. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function createAsyncInterator(arr: any[]): AsyncIterator { + let index = 0; + let len = arr.length; + return { + async next() { + return new Promise((resolve, reject) => { + if (index < len) { + resolve({ value: arr[index++], done: false }); + } else { + resolve({ value: undefined, done: true }); + } + }); + }, + }; +} + +let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; + +async function exp(arr: any[]) { + let asy = createAsyncInterator(arr); + let i = 0; + let fg; + while (true) { + if (fg == true) { + break; + } + await asy.next().then((v) => { + if (v.done == true) { + fg = true; + } + Assert.equal(v.value, arr[i++]); + return; + }); + } +} +exp(arr); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ffde8c1a83bad80a0a8dfa634992d3e00cbd7ff --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/async_iteration/async_iterators/async_iterators_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: > + an object that supports async iteration is said to be "iterable" if it has a Symbol.asyncIterator method that returns an AsyncIterator object. + options: + lib: es2018 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +async function asc(arr: any[]) { + let i = 0; + for await (let x of arr) { + Assert.equal(x, arr[i]); + i++ + } +} +let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; +asc(arr); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d62a1d01cc381fa0bf25fabe4873233b0667b3aa --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_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: > + es2015 also introduced "Generators", which are functions that can be used to yield partial computation results via the Iterator interface and the yield keyword. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function* generators() { + var i = 0; + while (true) { + yield i += 3; + if (i > 33) { + break; + } + } +} +let gen = generators(); +let c = 0; +while (true) { + let next = gen.next(); + if (next.done == true) { + break; + } + Assert.equal(next.value, c += 3); +} diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a529605fbda30c5d811e07b2d907be47f128916 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.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: > + generators can also internally delegate calls to another iterable through yield *. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr2 = ["A", "B", "C", "D", "E", "F"]; +function* generators2() { + yield* arr2; + let s = arr2[arr2.length - 1].charCodeAt(0); + for (let i = 0; i < 20; i++) { + s++; + yield String.fromCharCode(s); + } +} +let gen2 = generators2(); +let c = 65; +while (true) { + let next = gen2.next(); + if (next.done == true) { + break; + } + Assert.equal(next.value, String.fromCharCode(c++)); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea70a82050fbbedec6e28a617220403dad5a1190 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.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: > + es2015 introduced Iterator, which is an object that exposes three methods, next, return, and throw. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function createInterator(arr: any[]): Iterator { + let index = 0; + let len = arr.length; + return { + next() { + return index < len + ? { value: arr[index++], done: false } + : { value: undefined, done: true } + } + } +} +var arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]; +var iterator = createInterator(arr); +let i = 0; +while (true) { + let n = iterator.next(); + if (n.done == true) { + break; + } + Assert.equal(n.value, arr[i++]); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b4b1622d20f7c08d0b98282f1cc80bb104cc01c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_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: > + es2015 introduced Iterator, which is an object that exposes three methods, next, return, and throw. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr: string[] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; +let iter = arr[Symbol.iterator](); +let i = 0; +while (true) { + let n = iter.next(); + if (n.done == true) { + break; + } + Assert.equal(n.value, arr[i++]); +} +i = 0; +for (let x of arr) { + Assert.equal(x, arr[i++]); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..81461a48fd0e2c4466ee11920331ce01801c1629 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_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: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + a type parameter is deemed optional if it has a default.Required type parameters must not follow optional type parameters. + when specifying type arguments, you are only required to specify type arguments for the required type parameters. Unspecified type parameters will resolve to their default types.If a default type is specified and inference cannot choose a candidate, the default type is inferred. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +type ALL = string | number | boolean | object; +function fun1(v: V, t: T) { + return JSON.stringify({ v, t }); +} +let f1 = fun1(1, [1, 0]); +Assert.equal(f1, "{\"v\":1,\"t\":[1,0]}") + +let f2 = fun1("A", 0); +Assert.equal(f2, "{\"v\":\"A\",\"t\":0}"); + +let f3 = fun1("A", [1, "A"]); +Assert.equal(f3, "{\"v\":\"A\",\"t\":[1,\"A\"]}"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..de7f4fb6405096a61d6e9cab23fb1633ae0a3530 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_2.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: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + a class or interface declaration that merges with an existing class or interface declaration may introduce a default for an existing type parameter. + a class or interface declaration that merges with an existing class or interface declaration may introduce a new type parameter as long as it specifies a default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class CA{ + ca0: V; + ca1: T; + ca2: U; + constructor(ca0: V, ca1: T, ca2: U) { this.ca0 = ca0; this.ca1 = ca1; this.ca2 = ca2; } + creatOBJ() { + let a = this.ca0; + let b = this.ca1; + let c = this.ca2; + return { a, b, c }; + } + toJSON() { + return JSON.stringify(this.creatOBJ()); + } +} +class CCA extends CA{ } +let cca1 = new CCA(0, [0, 0], "[0]"); +Assert.equal(cca1.toJSON(), "{\"a\":0,\"b\":[0,0],\"c\":\"[0]\"}"); +let cca2 = new CCA("A", ["A"], [["A"], ["B"]]); +Assert.equal(cca2.toJSON(), "{\"a\":\"A\",\"b\":[\"A\"],\"c\":[[\"A\"],[\"B\"]]}"); + +interface IB { + ia: V; + ib: T; + ic: U; +} +interface CIB extends IB { } +let cib1: CIB = { ia: 0, ib: [0], ic: [[0], [1, 2]] }; +Assert.equal(JSON.stringify(cib1), "{\"ia\":0,\"ib\":[0],\"ic\":[[0],[1,2]]}"); +let cib2: CIB = { ia: "A", ib: ["A", "B"], ic: [["A"], ["B", "C"]] }; +Assert.equal(JSON.stringify(cib2), "{\"ia\":\"A\",\"ib\":[\"A\",\"B\"],\"ic\":[[\"A\"],[\"B\",\"C\"]]}"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fcb7540ac2eaffd96ab592aea57d6f2a6afb190 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.3/generic_parameter_defaults/generic_parameter_defaults_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: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + default types for a type parameter must satisfy the constraint for the type parameter, if it exists. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface Length { + length: number; +} +type STRS = string | string[] +function fun3(s: T) { + return s.length; +} +Assert.equal(fun3("ABC"), 3); +Assert.equal(fun3([1, 2, 3, 4]), 4); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..fd34d118eeaab298ee9235c5e5cfd30f990bad5a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.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: Dynamic import expressions are a new feature and part of ECMAScript that allows users to asynchronously request a module at any arbitrary point in your program. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' +async function func(x: number, y: number): Promise { + const add = await import("./lib.js"); + const sum = add.add(x, y); + return sum; +} + +func(10, 20).then((sum) => { + Assert.equal(30, sum); +}); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/lib.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/lib.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fca6c85acc534c2a1d641c176a3394a98b3efad --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/1_dynamic_import_expressions/lib.ts @@ -0,0 +1,19 @@ +/* + * 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 function add(x: number, y: number) { + return x + y; +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/2_string_enums/string_enums_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/2_string_enums/string_enums_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4b851149fdee7df198f8d7a786240ea45056aab --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/2_string_enums/string_enums_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: TypeScript 2.4 now allows enum members to contain string initializers. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +enum Option { + END = "End", + START = "START", + STOP = "STOP" +}; + +Assert.equal(Option.END, "End"); +Assert.equal(Option.START, "START"); +Assert.equal(Option.STOP, "STOP"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9b28a447bbb597b49bcf6c70cb71828f99763e0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.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: TypeScript 2.4 introduces a few wonderful changes around the way generics are inferred. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +function func(a: number, b: string) { + return a + b; +} +Assert.isString(func(10, 'a')); + +type T1 = (x: T, y: U) => [T, U]; +type T2 = (x: T, y: T) => [T, T]; +function f(a: T1, b: T2) { + b = a; + Assert.isTrue(b == a); +} + +let a: T1 = function funA(x: T, y: U): [T, U] { + return [x, y]; +} +let b: T2 = function funB(x: T, y: T): [T, T] { + return [x, y]; +} +f(a, b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9db170dbefb0384cdc1b2c6992ee5223bdda62b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.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: TypeScript 2.4 introduces tightens this up when relating two callback types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function f(arg: U): U{ + if (typeof arg === 'number') { + return arg; + } + return arg; +} +function func(a: T, callback: (arg: U) => U){ + return callback(a); +} + +let a = func(5, f); +let b = func('a', f); +Assert.isNumber(a); +Assert.isString(b); +b = a; +Assert.equal(a, b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.4/5_weak_type_detection/5_weak_type_detection.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/5_weak_type_detection/5_weak_type_detection.ts new file mode 100644 index 0000000000000000000000000000000000000000..3b27b4915e50e80d74abcf6b845d18c1f51b6353 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.4/5_weak_type_detection/5_weak_type_detection.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: TypeScript 2.4 introduces the concept of "weak types". Any type that contains nothing but a set of all-optional properties is considered to be weak. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface I { + month?: string; + type?: number; + value?: number; +} + +function check(i: I) { + if (i !== undefined && i !== null) { + return true; + } else { + return false; + } +} + +const obj1 = { + month: "January" +}; +Assert.isTrue(check(obj1)); + + +const obj2: { + [index: string]: { mem: string }; +} = {}; +Assert.isTrue(check(obj2)); + +const obj3 = { + str: "string", + trueOrFalse: true +} as I; +Assert.isTrue(check(obj3)); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.5/1_optional_catch_clause_variables.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.5/1_optional_catch_clause_variables.ts new file mode 100644 index 0000000000000000000000000000000000000000..9584bf048ce260da023d8e3b93bcccc30be54e48 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.5/1_optional_catch_clause_variables.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: TypeScript 2.5 implements a new ECMAScript feature that allows users to omit the variable in catch clauses. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +function func(str: string) { + try { + return JSON.parse(str); + } catch { + return str + " is Error JSON"; + } +} + +let arrjson: string = '[1,3,5]'; +let arr = func(arrjson); +Assert.equal(arr[0], 1); +Assert.equal(arr[1], 3); +Assert.equal(arr[2], 5); + +let str: string = 'string'; +Assert.equal(func(str), 'string is Error JSON'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.ts new file mode 100644 index 0000000000000000000000000000000000000000..455e574f5bc012505c667a929b44f472f0a05372 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.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 // @ts-ignore comment suppresses all errors that originate on the following line. + It is recommended practice to have the remainder of the comment following @ts-ignore explain which error is being suppressed. + Please note that this comment only suppresses the error reporting, and we recommend you use this comments very sparingly. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function addString(str: string, str2: string): string { + str += ""; + str2 += ""; + return str + str2; +} +// @ts-ignore +let n: number = addString(114, 514); +Assert.isString(n); +Assert.equal(n, "114514"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/10_numeric_separators/10_numeric_separators.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/10_numeric_separators/10_numeric_separators.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e1a4e3a17de06ac6aa0fbb695d40e8c3b842aec --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/10_numeric_separators/10_numeric_separators.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: TypeScript 2.7 brings support for ES Numeric Separators. Numeric literals can now be separated into segments using _. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +const num999 = 999_999_999; +const xnum = 0xFF_00_FF; +const bnum = 0b10_01; + +Assert.equal(999999999, num999); +Assert.equal(0xFF00FF, xnum); +Assert.equal(9, bnum); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/1_constant_named_properties/1_constant_named_properties.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/1_constant_named_properties/1_constant_named_properties.ts new file mode 100644 index 0000000000000000000000000000000000000000..417bd3952812c4acca1c1ecc68d7a80ed4b36c40 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/1_constant_named_properties/1_constant_named_properties.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: TypeScript 2.7 adds support for declaring const-named properties on types including ECMAScript symbols. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +const sym = Symbol(); +interface I{ + [sym](func: Function): Function; +} +class C implements I{ + [sym](func: Function) { + return func; + } +} +let f = new C(); +Assert.isFunction(f[sym]); + + +const num = 5; +const str = 'a'; + +let obj = { + [num]: 5, + [str]: "string" +} + +let para1 = obj[num]; +let para2 = obj[str]; +Assert.isNumber(para1); +Assert.isString(para2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/2_unique_symbol/2_unique_symbol.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/2_unique_symbol/2_unique_symbol.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf3251ec6c11b9b93e83af04df4117b6434f9718 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/2_unique_symbol/2_unique_symbol.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: To enable treating symbols as unique literals a new type unique symbol is available. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +const usym1: unique symbol = Symbol(); +const usym2: unique symbol = Symbol.for("Bar"); + +let t1: typeof usym1 = usym1; +let t2: typeof usym2 = usym2; + +class C { + static readonly StaticUsym: unique symbol = Symbol(); +} + +Assert.isTrue('symbol' === typeof t1); +Assert.isTrue('symbol' === typeof t2); +Assert.isTrue('symbol' === typeof C.StaticUsym); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/3_strict_class_initialization/3_strict_class_initialization.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/3_strict_class_initialization/3_strict_class_initialization.ts new file mode 100644 index 0000000000000000000000000000000000000000..f47984ee0dc02c5f3fbfabaf6cb72c6ec6ba1167 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/3_strict_class_initialization/3_strict_class_initialization.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: TypeScript 2.7 introduces a new flag called strictPropertyInitialization. This flag performs checks to ensure that each instance property of a class gets initialized in the constructor body, or by a property initializer. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class C { + t01: number = 1; + t02!: number; + t03 = "hello"; + t04: boolean = true; + + constructor() { + this.initialize(); + } + + initialize() { + this.t02 = 2; + } +} + +let c = new C(); +Assert.equal(1, c.t01); +Assert.equal(2, c.t02); +Assert.equal('hello', c.t03); +Assert.isTrue(c.t04); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.ts new file mode 100644 index 0000000000000000000000000000000000000000..20eb817087224d802301ba302e150dcf73974e38 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.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: The definite assignment assertion is a feature that allows a ! to be placed after instance property and variable declarations to relay to TypeScript that a variable is indeed assigned for all intents and purposes, even if TypeScript's analyses cannot detect so. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +function func(arg: number | undefined) { + let sum = arg! + arg!; + return sum; +} +let num = func(5); +Assert.isFalse(Number.isNaN(num)); + +let x: number; +let add = x! + x!; +Assert.isTrue(Number.isNaN(add)); + +let num1: number; +later1(); +let sum1 = num1! + num1!; +function later1() { + num1 = 5; +} +Assert.equal(sum1, 10); + +let num2!: number; +later2(); +let sum2 = num2 + num2; +function later2() { + num2 = 5; +} +Assert.equal(sum2, 10); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/5_fixed_length_tuples/5_fixed_length_tuples.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/5_fixed_length_tuples/5_fixed_length_tuples.ts new file mode 100644 index 0000000000000000000000000000000000000000..4604dd74701c8f6b1a913a04168fae2003757bd3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/5_fixed_length_tuples/5_fixed_length_tuples.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: tuple types now encode their arity into the type of their respective length property. This is accomplished by leveraging numeric literal types, which now allow tuples to be distinct from tuples of different arities. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface NSTup extends Array { + 0: number; + 1: string; + length: 2; +} + +const numstr01: NSTup = [1, "string"]; + +interface NS extends Array { + 0: number; + 1: string; +} + +let numstr02: NS = [2, "string"]; +numstr02= [2, "string", 3, "string2"]; + +Assert.equal(numstr01.length, 2); + +Assert.equal(numstr02.length, 4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4531e87684a3a3f301b0286d74746bad7183a60 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.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: TypeScript 2.7 improves type inference for multiple object literals occurring in the same context. When multiple object literal types contribute to a union type, we now normalize the object literal types such that all properties are present in each constituent of the union type. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +let obj1 = [{ a: 1, b: 2 }, { a: "abc" }, {}][0]; +obj1.a = 5; +Assert.isNumber(obj1.a); +obj1.a = 'a'; +Assert.isString(obj1.a); +obj1.a = undefined; +Assert.isUndefined(obj1.a); + +obj1.b = 6; +Assert.isNumber(obj1.b); +obj1.b = undefined; +Assert.isUndefined(obj1.b); + +function fun(...args: T[]): T { + return args[1]; +}; +let obj2 = fun({ a: 1, b: 2 }, { a: "abc", b: "ABC" }, {}); + +obj2.a = 5; +Assert.isNumber(obj2.a); +obj2.a = 'a'; +Assert.isString(obj2.a); +obj2.a = undefined; +Assert.isUndefined(obj2.a); + +obj2.b = 6; +Assert.isNumber(obj2.b); +obj2.b = 'b'; +Assert.isString(obj2.b); +obj2.b = undefined; +Assert.isUndefined(obj2.b); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/7_improved_handling/7_improved_handling.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/7_improved_handling/7_improved_handling.ts new file mode 100644 index 0000000000000000000000000000000000000000..68cd0868514bf0c11c5e374e995ca091f55a4f86 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/7_improved_handling/7_improved_handling.ts @@ -0,0 +1,101 @@ +/* + * 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 2.7 improves the handling of structurally identical classes in union types and instanceof expressions: + Structurally identical, but distinct, class types are now preserved in union types (instead of eliminating all but one). + Union type subtype reduction only removes a class type if it is a subclass of and derives from another class type in the union. + Type checking of the instanceof operator is now based on whether the type of the left operand derives from the type indicated by the right operand (as opposed to a structural subtype check). + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class C1{ + num: number; + constructor(num: number) { + this.num = num; + } +} +class C2 extends C1{ + str: string; + constructor(num: number,str:string) { + super(5); + this.num = num; + this.str = str; + } +} +class C3 extends C1{ + boo: boolean; + constructor(num: number,boo:boolean) { + super(6); + this.num = num; + this.boo = boo; + } +} +class C4 extends C1{ + obj: object; + constructor(num: number, obj: object) { + super(7); + this.obj = obj; + this.num = num; + } +} + +let t1 = !true ? new C1(10) : new C2(11, 'a'); +function func1(arg: C1 | C2 | C3 | C4) { + if (arg instanceof C1) { + arg.num = 20; + return arg.num + } + return false; +} +Assert.equal(func1(t1), 20); + +let t2 = !true ? new C2(12, 'b') : new C3(13, false); +function func2(arg: C1 | C2 | C3 | C4) { + if (arg instanceof C3) { + arg.num = 20; + return arg.num + } + return false; +} +Assert.equal(func2(t2), 20); + +let t3 = !true ? new C3(14, true) : new C4(15, { a: 'a' }); +function func3(arg: C1 | C2 | C3 | C4) { + if (arg instanceof C4) { + arg.num = 20; + return arg.num + } + return false; +} +Assert.equal(func3(t3), 20); + + +function func(x: C2 | C3 | C4) { + if (x instanceof C2 ) { + return x.str; + } else if (x instanceof C3) { + return x.boo; + } else { + return x.obj + } +} +Assert.isString(func(new C2(1, 'C2'))); +Assert.isBoolean(func(new C3(2, true))); +Assert.isObject(func(new C4(3, { o: 'obj' }))); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..5390cca33a5401cd87e3358a726a2aa86bc692fc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.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 in operator now acts as a narrowing expression for types. + For a n in x expression, where n is a string literal or string literal type and x is a union type, the “true” branch narrows to types which have an optional or required property n, and the “false” branch narrows to types which have an optional or missing property n. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +type myType = { [T in 'key1' | 'key2' | 'key3']: string }; +let mt: myType = { + key1: 'a', + key2: 'b', + key3: 'c' +} +Assert.isObject(mt); + +interface I1{ + name: string; + age: number; +} +interface I2{ + height: number; +} +let i1: I1 = { + name: 'xiao', + age: 18 +}; +let i2: I2 = { + height: 180 +}; +function func(arg: I1 | I2) { + if ('name' in arg) { + arg.age = 20; + return arg.age; + } + arg.height = 185; + return arg.height; +} +Assert.equal(func(i1), 20); +Assert.equal(func(i2), 185); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Defaulted_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Defaulted_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..a52a3eb2cd4ee8232ad85d2fb4dc32ea87aa19e0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Defaulted_declarations.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: > + Defaulted declarations + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +let x; +x = x || 1; +Assert.equal(x, 1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/IIFEs_as_namespace_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/IIFEs_as_namespace_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..cddb2330305e6998fa961bad02848fb7e8b8de86 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/IIFEs_as_namespace_declarations.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: > + IIFEs as namespace declarations + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +let C:any = (function() { + function C(this:any, net:any):void { + this.x = net; + } + return C; +})(); +let c = new C(1); + +Assert.equal(c.x, 1) \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d4042482d6fa3f2ce5ac79225958e26543781e3b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_1.ts @@ -0,0 +1,64 @@ +/* + * 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: > + With TypeScript 2.8 keyof applied to an intersection type is transformed to a union of keyof applied to each intersection constituent. + In other words, types of the form keyof (A & B) are transformed to be keyof A | keyof B. This change should address inconsistencies with inference from keyof expressions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +type A = { a: string }; +type B = { b: number }; + +type TAB = keyof (A & B); +let tab: TAB; +tab = "a"; +tab = "b"; + +type TTB = keyof (T & B); +let ttb: TTB; +ttb = "b"; +ttb = "valueOf"; + +type TAU = keyof (A & U); +let tau: TAU; +tau = "a"; +tau = "charCodeAt"; + +type TTU = keyof (T & U); +let ttu: TTU; +ttu = "charAt"; +ttu = "toString"; + +type IsEqual = + (() => T extends X ? 1 : 2) extends + (() => T extends Y ? 1 : 2) ? true : false + + +let f1: IsEqual = true; +Assert.isTrue(f1); + +let f2: IsEqual, keyof boolean | keyof B> = true; +Assert.isTrue(f2); + +let f3: IsEqual, keyof A | keyof string> = true; +Assert.isTrue(f3); + +let f4: IsEqual, keyof number | keyof string> = true; +Assert.isTrue(f4); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..77c1f130f2a2c14e7ea4cb52642d7b487d8c94ce --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/Improved_keyof_with_intersection_types_2.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: > + With TypeScript 2.8 keyof applied to an intersection type is transformed to a union of keyof applied to each intersection constituent. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type myType1 = 'a' | 4; + type myType2 = { str: string }; + interface I { + mem: string; + } + + type TA = keyof (myType1 & myType2); + type TB = keyof (T & myType2); + type TC = keyof (myType1 & U); + type TD = keyof (T & U); + type TE = TB; + type TF = TC; + type TG = TD; + + let a: TA = 'str'; + a = 'toString'; + a = 'valueOf'; + Assert.equal(a, 'valueOf'); + let b: TE = "str"; + b = 'toString'; + b = 'valueOf'; + Assert.equal(b, 'valueOf'); + let c: TF = "str"; + c = 'toString'; + c = 'valueOf'; + Assert.equal(c, 'valueOf'); + let d: TG = "str"; + d = 'toString'; + d = 'valueOf'; + Assert.equal(d, 'valueOf'); + + type T11 = keyof (I & myType2); + type T22 = keyof (T & myType2); + type T33 = keyof (I & U); + type T44 = keyof (T & U); + type T55 = T22; + type T66 = T33; + type T77 = T44; + + let aa: T11 = 'mem'; + aa = 'str'; + Assert.equal(aa, 'str'); + let bb: T55 = 'mem'; + bb = 'str'; + Assert.equal(bb, 'str'); + let cc: T66 = 'mem'; + cc = 'str'; + Assert.equal(cc, 'str'); + let dd: T77 = 'mem'; + dd = 'str'; + Assert.equal(dd, 'str'); + +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/conditional_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..ca35f0e50db08c8d421ade8b79d80885c1955d46 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/conditional_types.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: > + Conditional Types. +module: ESNext +isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +interface I1{ + str: string; +} +interface I2{ + num: number; +} +type mT = T extends I1 ? boolean : T extends I2 ? number : string; +function func1>(arg: T) { + return arg; +} +Assert.isBoolean(func1(true)); +function func2>(arg: T) { + return arg; +} +Assert.isNumber(func2(5)); +function func3>(arg: T) { + return arg; +} +Assert.isString(func3('a')); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/distributive_conditional_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/distributive_conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..d0b067b48fef6f77c51b7cfe2ccfa7c84f4d96f9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/distributive_conditional_types.ts @@ -0,0 +1,67 @@ +/* + * 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: > + Distributive conditional types +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../suite/assert.js" +type TypeGather = + T extends string ? string : + T extends number ? number : + T extends boolean ? boolean : + T extends undefined ? undefined : + T extends Function ? Function : + object; + + +type TSF = TypeGather void)>; +type TSSA = TypeGather; +type TSANA = TypeGather; + +type GatherValue = { value: T }; +type GatherArray = { array: T[] }; +type Gather = T extends any[] ? GatherArray : GatherValue; + +type TGS = Gather; +type TGNA = Gather; +type TGSNA = Gather; + +let a: TSF = 's'; +let b: TSF = (() => { }); +let c: TSSA = 's'; +let d: TSSA = ['s']; +let e: TSSA = undefined; +let f: TSANA = ['s']; +let g: TSANA = [1]; +let h: TGS = { value: "s" }; +let i: TGNA = { array: [1] }; +let j: TGSNA = { value: "s" }; +let k: TGSNA = { array: [1] }; + +Assert.equal(typeof a, 'string'); +Assert.equal(typeof b, 'function'); +Assert.equal(typeof c, 'string'); +Assert.equal(typeof d, 'object'); +Assert.equal(typeof e, 'undefined'); +Assert.equal(typeof f, 'object'); +Assert.equal(typeof g, 'object'); +Assert.equal(typeof h, 'object'); +Assert.equal(typeof i, 'object'); +Assert.equal(typeof j, 'object'); +Assert.equal(typeof k, 'object'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/filter_union_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/filter_union_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..18ba9b10f9a994b183816bd970f411cb50676cd0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/filter_union_types.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: > + The distributive property of conditional types can conveniently be used to filter union types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type TA = T extends U ? never : T; + type TB = T extends U ? T : never; + + type T0 = TA; + type T1 = TB; + + let a: T0 = 'b'; + let b: T0 = true; + let c: T1 = 1; + + Assert.equal(typeof a, 'string'); + Assert.equal(typeof b, 'boolean'); + Assert.equal(typeof c, 'number'); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf24b3e1238fb1fde6e2954d33d01170eb071639 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_1.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: > + TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. + Specifically, a readonly or ? property modifier in a mapped type can now be prefixed with either + or - to indicate that the modifier should be added or removed. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js"; + +type RU = { +readonly [key in "A" | "B" | "C"]-?: number }; +type NR = { -readonly [key in 1 | 2 | 3]+?: string }; + +let ru: RU = { 'A': 1, 'B': 2, 'C': 3 }; +let nr: NR = { 1: 'A', 3: 'C' }; +nr[1] = 'Z'; +Assert.equal(JSON.stringify(ru), '{"A":1,"B":2,"C":3}'); +Assert.equal(JSON.stringify(nr), '{"1":"Z","3":"C"}'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3813580fec41865bc892de66195bb8c889364a91 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/improved_control_over_mapped_type_modifiers_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: > + Using this ability, lib.d.ts now has a new Required type. + This type strips ? modifiers from all properties of T, thus making all properties required. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js"; + +type ABCU = { 'A'?: number, 'B'?: number, 'C'?: number, 'D'?: number }; +type ABC = Required; + +let abcu: ABCU = { 'C': 3 }; +let abc: ABC = { A: 1, B: 2, C: 3, D: 4 }; +Assert.equal(JSON.stringify(abcu), '{"C":3}'); +Assert.equal(JSON.stringify(abc), '{"A":1,"B":2,"C":3,"D":4}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/predefined_conditional_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/predefined_conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..bbf58c8b5fdc511d3382078c0ba5d812996d1678 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/predefined_conditional_types.ts @@ -0,0 +1,75 @@ +/* + * 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: > + Predefined conditional types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + + type TypeA = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; + + type TypeB = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">; + + + type TypeC = Exclude void), Function>; + + type TypeD = Extract void), Function>; + + function fun(s: string) { + return { a: 1, b: s }; + } + + class C { + x = 0; + y = 0; + } + + + type TypeE = ReturnType; + + type TypeF = InstanceType; + + let a: TypeA = "b"; + let b: TypeA = "d"; + let c: TypeB = "a"; + let d: TypeB = "c"; + let e: TypeC = "c"; + let f: TypeD = ((): string => { return 's' }); + let x: TypeE = { a: 1, b: 's' } + let y: TypeF = { x: 1, y: 2 } + + Assert.equal(a, 'b'); + Assert.equal(b, 'd'); + Assert.equal(c, 'a'); + Assert.equal(d, 'c'); + Assert.equal(e, 'c'); + Assert.equal(typeof f, "function"); + Assert.equal(x.a, 1); + Assert.equal(y.x, 1); + + type G = number[] | number | null | undefined + type TypeG = NonNullable; + let g: TypeG; + g = [0, 1, 2]; + Assert.equal(g.length, 3); + g = -1; + Assert.equal(g, -1); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/prototype_assignment.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/prototype_assignment.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f041e19a8b7dde591dcfd5a3611b76e2a0c4675 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/prototype_assignment.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: > + You can assign an object literal directly to the prototype property. Individual prototype assignments still work too. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +let q: number = 0; +let c: any = function (p: number) { q = p; } +c.sub = { + m() { + Assert.equal(q, 5); + } +} +c(5); +c.sub.m(); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.8/type_inference.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..acbaa22bfeb7f260cb4a0883e5cdad25d429c61c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.8/type_inference.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: > + Type inference in conditional types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type Type00 = + T extends (infer U)[] ? U : + T extends (...args: any[]) => infer U ? U : + T extends Promise ? U : + T; + + type TS = Type00; + type TSA = Type00; + type TF = Type00<() => string>; + type TPS = Type00>; + type TTPS = Type00[]>>; + + let a: TS = 's'; + let b: TSA = 's'; + let c: TF = 's'; + let d: TPS = 's'; + let e: TTPS = 's'; + + Assert.equal(typeof a, 'string'); + Assert.equal(typeof b, 'string'); + Assert.equal(typeof c, 'string'); + Assert.equal(typeof d, 'string'); + Assert.equal(typeof e, 'string'); + + type F = T extends { a: infer U, b: infer U } ? U : never; + type TF1 = F<{ a: string, b: string }>; + type TF2 = F<{ a: string, b: number }>; + + let f: TF1 = 's'; + let g: TF2 = 's'; + let h: TF2 = 1; + + Assert.equal(typeof f, 'string'); + Assert.equal(typeof g, 'string'); + Assert.equal(typeof h, 'number'); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.9/generic_type_arguments_in_tt/generic_type_arguments_in_tt.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/generic_type_arguments_in_tt/generic_type_arguments_in_tt.ts new file mode 100644 index 0000000000000000000000000000000000000000..e635600b3397a02b91831c14891273389325dfd1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/generic_type_arguments_in_tt/generic_type_arguments_in_tt.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: TypeScript 2.9 allows passing generic type arguments to tagged template strings. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +let jianbing = "jianbing" +function kitchen(xx: TemplateStringsArray, value: string) { + Assert.equal(2, xx.length); +} +kitchen`lunch_is${jianbing}!`; +function f(yy: TemplateStringsArray, args: T): T { + Assert.equal(2, yy.length); + return args; +}; +let a = f `lunch_is${jianbing}!`; +Assert.equal("jianbing", a); +let b = f `lunch_is${"dessert"}!`; +Assert.equal("dessert", b); +Assert.equal("dessert", b); +let c = f`sss${2}`; +Assert.equal(2, c); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.9/import_types/import_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/import_types/import_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..7306d9cd8d7554718ec7f9ba89be50cb147a0bd1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/import_types/import_types.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: > + Modules can import types declared in other modules. But non-module global scripts cannot access types declared in modules. Enter import types. + Using import("mod") in a type annotation allows for reaching in a module and accessing its exported declaration without importing it. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +function func1(i: import("../module").I) { + Assert.equal(5, `${i.num}`); + Assert.equal(5, i.num); +} +let i: import("../module.js").I = { + num: 5 +} +func1(i); +function func(p: import("../module").C) { + Assert.equal('aa', `${p.str}`); + Assert.equal('aa', p.str); +} +let p: import("../module.js").C = { + str: "aa", +}; +func(p); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.9/module.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/module.ts new file mode 100644 index 0000000000000000000000000000000000000000..d38ce3a37c4f00aabba6307613b1a78f6f0ef98a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/module.ts @@ -0,0 +1,23 @@ +/* + * 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 declare class C { + str: string; +}; + +export declare interface I{ + num: number; +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..52f705f74cece73d75c58878df44cd296f233e3a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.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: > + TypeScript 2.9 adds support for number and symbol named properties in index types and mapped types. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +const str = "x"; +const num = 1; +const sym = Symbol(); + +type Obj = { + 2: string; + [str]: string; + [num]: string; + [sym]: string; +} +type T1 = keyof Obj; +let x1: T1 = "x"; +let x2: T1 = sym; +let x3: T1 = 1; +let x4: T1 = 2; +Assert.equal("x", x1); +Assert.isString(x1); +Assert.equal(sym, x2); +Assert.isSymbol(x2); +Assert.equal(1, x3); +Assert.isNumber(x3); +Assert.equal(2, x4); +Assert.isNumber(x4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f67dfe55b751736f36ae5931c864a0e87743698 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/2.9/support_number_and_symbol_nam/1_support_number_and_symbol_nam_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: > + TypeScript 2.9 adds support for number and symbol named properties in index types and mapped types. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +const str = 'x' +const num = 1 +const sym = Symbol() +type Objs1 = { + [str]: string, + [num]: number, + [sym]: symbol +} +type Types = { + [P in keyof T]: T[P] +} +let objs2: Types = { + x: 'xx', + 1: 11, + [sym]: Symbol() +} +Assert.isString(objs2[str]); +Assert.isNumber(objs2[num]); +Assert.isSymbol(objs2[sym]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/new_unknown_top_type/new_unknown_top_type.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/new_unknown_top_type/new_unknown_top_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..d13540ea67233a96d0349dc80f77bd83f4f951bf --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/new_unknown_top_type/new_unknown_top_type.ts @@ -0,0 +1,117 @@ +/* + * 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 3.0 introduces a new top type unknown. unknown is the type-safe counterpart of any. + anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. + likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +type T1 = unknown & undefined; +let t1: T1 = undefined; +Assert.isUndefined(t1); +type T2 = unknown & string; +let t2: T2 = 'a'; +Assert.isString(t2); +type T3 = unknown | undefined; +let t3: T3 = undefined; +Assert.isUndefined(t3); +type T4 = unknown | null | undefined; +let t4: T4 = null; +t4 = undefined; +Assert.isUndefined(t4); +type T5 = unknown | string; +let t5: T5 = 'a'; +Assert.isString(t5); +type T6 = T & {}; +let t6: T6 = { arg: 'arg' }; +Assert.isObject(t6); +type T7 = T | {}; +let t7: T7 = 10; +t7 = {}; +Assert.isObject(t7); +type T8 = T & unknown; +let t8: T8 = 10; +Assert.isNumber(t8); +type T9 = T | unknown; +let t9: T9 = 10; +Assert.isNumber(t9); +type T10 = unknown extends T ? true : false; +let t10: T10 = false; +Assert.isFalse(t10); +type T11 = T extends unknown ? true : false; +let t11: T11 = true; +Assert.isTrue(t11); +type T12 = never extends T ? true : false; +let t12: T12 = true; +Assert.isTrue(t12); +type T13 = T extends never ? true : false; +let t13: T13 = false; +Assert.isFalse(t13); +function func1(x: unknown): unknown { + x = 10; + return x; +} +Assert.equal(func1(0), 10); +function func2(x: unknown) { + return typeof x; +} +Assert.equal(func2("N"), "string"); +function func3(x: unknown) { + if (x instanceof Error) { + return 0; + } + return false; +} +let e: Error = Error(); +Assert.isNumber(func3(e)); +type T14 = { [P in keyof T]: number }; +type T15 = T14; +type T16 = T14; +let t15: T15 = {}; +Assert.isObject(t15); +let t16: T16 = {}; +Assert.isObject(t16); +function func4(pAny: any, pundefined: undefined, pT: T) { + let x: unknown; + x = 123; + Assert.isNumber(x); + x = "hello"; + Assert.isString(x); + x = [1, 2, 3]; + x = new Error(); + x = x; + x = pAny; + x = pundefined; + x = pT; +} +func4(1024, undefined, "A"); +function fun5(x: unknown) { + let v: unknown = x; + return v; +} +Assert.equal(fun5(1024), 1024); +function fun6() { + let a: unknown; + let b = a; + b = 2; + return b; +} +Assert.equal(fun6(), 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..224eb8e721bc62fd31e2636387e269a513427ba0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/optional_elements_in_tuple_types/optional_elements_in_tuple_types.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: > + Optional elements in tuple types , Rest elements in tuple types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let o1:[number, ...string[]] = [1, '1', '2'] +let o2:[number, ...string[]] = [1, '1', '2', '3'] +Assert.equal(o1.length, 3) +Assert.equal(o2.length, 4) + +let o3: [string, string?, number?] +o3 = ['test', "hello", 1]; +Assert.equal(o3.length, 3) +o3 = ['test', "hello"]; +Assert.equal(o3.length, 2) +o3 = ['test']; +Assert.equal(o3.length, 1) + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/reference_lib/reference_lib.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/reference_lib/reference_lib.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f4d332536224eb77c269f67ee325c6b1c01e9c3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/reference_lib/reference_lib.ts @@ -0,0 +1,26 @@ +/// +/* + * 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 adds a new triple-slash-reference directive (/// ), allowing a file to explicitly include an existing built-in lib file. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js"; +let sym: symbol = Symbol("es2015"); +Assert.isSymbol(sym); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ca47e82dfbe2c41ab00516081659d68516e4dff --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.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: > + Rest parameters with tuple types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function test1(...ar:[string,number,boolean]):number { + return 0 +} +function test2(ar:string,ar1: number,ar2:boolean):number { + return 1 +} + +Assert.equal(test1("11",2,true), 0); +Assert.equal(test2("11",2,true), 1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..f64b88960f716a595cba05f111ebba716daa7f0c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.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: > + a rest parameter is permitted to have a generic type that is constrained to an array type, and type inference can infer tuple types for such generic rest parameters. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function fun1(f: (x: T, ...args: U) => V): (...args: U) => V { + function f2(...args: U) { let json = JSON.stringify(args); return json as unknown as V; } + return f2; +}; +function fun2(x: number, y: string, z: boolean): string { + let a: any[] = [x, y, z]; + let json = JSON.stringify(a); + return json; +} +let a = fun2(5, "A", true); +let ff1 = fun1(fun2); +let b = ff1("B", true); +let ff2 = fun1(ff1); +let c = ff2(true); +let ff3 = fun1(ff2); +let d = ff3(); +Assert.equal(a, "[5,\"A\",true]"); +Assert.equal(b, "[\"B\",true]"); +Assert.equal(c, "[true]"); +Assert.equal(d, "[]"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..7dd6d242ccb2dbbb5ca1295fea75b8ad1f77bcf9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.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: > + tuple types now permit a ? postfix on element types to indicate that the element is optional.In strictNullChecks mode, a ? modifier automatically includes undefined in the element type, similar to optional parameters. + a tuple type permits an element to be omitted if it has a postfix ? modifier on its type and all elements to the right of it also have ? modifiers. + The length property of a tuple type with optional elements is a union of numeric literal types representing the possible lengths. For example, the type of the length property in the tuple type [number, string?, boolean?] is 1 | 2 | 3. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let opt: [number, string?, boolean?, number[]?]; +opt = [1024, "str", true, [1, 2]]; +Assert.equal(JSON.stringify(opt), "[1024,\"str\",true,[1,2]]"); +Assert.equal(opt.length, 4); +opt = [1024, "str", true]; +Assert.equal(JSON.stringify(opt), "[1024,\"str\",true]"); +Assert.equal(opt.length, 3); +opt = [1408, "N"]; +Assert.equal(JSON.stringify(opt), "[1408,\"N\"]"); +Assert.equal(opt.length, 2); +opt = [0]; +Assert.equal(JSON.stringify(opt), "[0]"); +Assert.equal(opt.length, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..447825ad5ab6683781a08e747da28b1440adfc75 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.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 last element of a tuple type can be a rest element of the form ...X, where X is an array type. A rest element indicates that the tuple type is open-ended and may have zero or more additional elements of the array element type. + the type of the length property of a tuple type with a rest element is number. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function fun1(...args: T): T { + return args; +} +function fun2() { + let arr: number[] = []; + for (let i = 0; i <= 10; i++) { + arr.push(i); + } + return arr; +} +const numbers: number[] = fun2(); +const t1 = fun1("AAA", 1, true); +const t2 = fun1("BBB", ...numbers); +const t3 = ["A", true, ...numbers, false]; +Assert.equal(JSON.stringify(t1), "[\"AAA\",1,true]"); +Assert.equal(JSON.stringify(t2), "[\"BBB\",0,1,2,3,4,5,6,7,8,9,10]"); +Assert.equal(JSON.stringify(t3), "[\"A\",true,0,1,2,3,4,5,6,7,8,9,10,false]"); +Assert.isNumber(t1.length); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..23c9f57e85fcf957b498ad2785ddffa8577fc4fd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.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: > + when a rest parameter has a tuple type, the tuple type is expanded into a sequence of discrete parameters. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function rfun1(a: string, b: number, c: boolean, ...v: [number, string, boolean]) { + return { number: v[0], string: v[1], boolean: v[2] }; +} +function rfun2(a: string, b: number, c: boolean, v1: number, v2: string, v3: boolean) { + return { number: v1, string: v2, boolean: v3 }; +} +var r1 = rfun1("a", 1, true, 1, "rfun", true); +var r2 = rfun2("a", 1, true, 1, "rfun", true); +var jr1 = JSON.stringify(r1); +var jr2 = JSON.stringify(r2); +Assert.equal(jr1, jr2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..532a39263aa4856841c75e023a568e7e8f0fe467 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.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: > + when a function call includes a spread expression of a tuple type as the last argument, the spread expression corresponds to a sequence of discrete arguments of the tuple element types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function fun1(...v: [number, string, boolean]) { + return { number: v[0], string: v[1], boolean: v[2] }; +} +const args: [number, string, boolean] = [11, "str", true]; +var v1 = fun1(...args); +var v2 = fun1(args[0], args[1], args[2]); +var v3 = fun1(11, "str", true); +var jv1 = JSON.stringify(v1); +var jv2 = JSON.stringify(v2); +var jv3 = JSON.stringify(v3); +var flag = false; +if (jv1 === jv2 && jv2 === jv3 && jv3 === jv1) { + flag = true; +} +Assert.isTrue(flag); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a18fba453762739c4ae4a6ef95cd58be3fec3736 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_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: > + In TypeScript 3.1, mapped object types over tuples and arrays now produce new tuples/arrays, + rather than creating a new type where members like push(), pop(), and length are converted. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +type Type = { + [k in keyof T]: Array; +}; +type tuple = [number, string, boolean]; +type tt = Type; +let cc: tt = [ + [0, 1, 2, 3], + ["a", "b", "c", "d"], + [false, true], +]; +Assert.equal(Array.isArray(cc), true); +Assert.equal(cc.length, 3); +cc.push([1, 2, 3]); +cc.push([false, false]); +cc.push(["hello", "world"]); +Assert.equal(cc.length, 6); + +let vv: any = cc.pop(); +Assert.equal(vv[0], "hello"); +Assert.equal(vv[1], "world"); +Assert.equal(cc.length, 5); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f18d78287b0d48e58f49f441ee694e61e49928ab --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.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: > + TypeScript 3.1 brings the ability to define properties on function declarations and const-declared functions, + simply by assigning to properties on these functions in the same scope. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function add(a: T, b: K) { + if (typeof a == "number" && typeof b == "number") { + return a + b; + } else { + return `${a}${b}`; + } +} + +add.description = "this"; +add.aa = 0; +add.bb = false; + +Assert.equal(add.description, "this"); +Assert.equal(add.aa, 0); +Assert.equal(add.bb, false); + +add.showInfo = () => { + return 1; +}; +Assert.equal(add.showInfo(), 1); + + +const link = () => { + return 11; +}; + +link.aa = 0; +link.bb = false; +link.cc = "this"; +link.showInfo = () => { + return 1; +}; +Assert.equal(link.cc, "this"); +Assert.equal(link.aa, 0); +Assert.equal(link.bb, false); +Assert.equal(link.showInfo(), 1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b802830d8076423b32b6b043311df8659fd99981 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_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: > + BigInts are part of an upcoming proposal in ECMAScript that allow us to model theoretically arbitrarily large integers. + Brings type-checking for BigInts, as well as support for emitting BigInt literals when targeting esnext. + BigInt support in TypeScript introduces a new primitive type called the 'bigint' (all lowercase). + You can get a bigint by calling the BigInt() function or by writing out a BigInt literal by adding an n to the end of any integer numeric litera. + options: + target: es2020 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let b1: bigint = 10n; +let b2: bigint = BigInt(10n); +Assert.equal(b1, b2); + +function func(bnum: bigint) { + for (let i = 0; i < 10; i++){ + bnum++; + } + return bnum; +} +Assert.equal(func(10n), 20n); + +class C{ + bigN: bigint + constructor(bigN: bigint) { + this.bigN = bigN; + } +} +let c = new C(10n); +Assert.equal(c.bigN, 10n); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a2f5c2267dadb8a05e2858ecf7170db501784e09 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_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: > + As specified in ECMAScript, mixing numbers and bigints in arithmetic operations is an error. + You'll have to explicitly convert values to BigInts. + options: + target: es2020 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +var x: number = 10; +var bi: bigint = 100n; + +var y = BigInt(x) * bi; +Assert.equal(y, 1000n); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..db0c1ad380b4c8cd2ab383f0fe5bc91217736cc1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/bigint/bigint_3.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: > + bigints produce a new string when using the typeof operator: the string "bigint". + Thus, TypeScript correctly narrows using typeof as you’d expect. + options: + target: es2020 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function num_or_bigint(x: number | bigint) { + if (typeof x === "bigint") { + return x; + } + else { + return x + 1; + } +} +var a = num_or_bigint(10); +var b = num_or_bigint(10n); +Assert.equal(a, 11); +Assert.equal(b, 10n); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..78a4d190014ff389f0a6241e820cd8f2ba7a945b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.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: > + Allows destructuring a rest binding from a generic variable. + This is achieved by using the predefined Pick and Exclude helper types from lib.d.ts, + and using the generic type in question as well as the names of the other bindings in the destructuring pattern. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func1(arg: T) { + let { ...restPara } = arg; + return restPara; +} +const obj1 = { x: 10, y: 'a' }; +const o1 = func1(obj1); +Assert.isObject(o1); +Assert.equal(JSON.stringify(o1), '{"x":10,"y":"a"}'); + +function func2(obj: T) { + let { str, ...rest } = obj; + return rest; +} + +const obj2 = { x: 10, y: 20, str: "string" }; +const o2 = func2(obj2); +Assert.isObject(o2); +Assert.equal(JSON.stringify(o2), '{"x":10,"y":20}'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cff47c0606c77fe68be0b824671a9283af2fd009 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.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: > + Object literals now allow generic spread expressions which now produce intersection types, + similar to the Object.assign function and JSX literals. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func(arg: T, other: U) { + return { ...arg, other }; +} +let f1 = { num: 5, str: 'a' }; +let f2 = { num: 8, str: 'b', boo: true }; + +Assert.isObject(func(f1, 'a')); +Assert.isObject(func(f2, 'b')); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0ea553829352314c026b13ac9b5d89f72287073f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_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: > + Property assignments and non-generic spread expressions are merged to the greatest extent possible on either side of a generic spread expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func(arg1: T, arg2: { str: string }) { + let obj = { n: 5, ...arg1, s: 's', ...arg2 }; + return obj; +} +let o1 = { + str: 'a' +}; +let f = func({ s: "string" }, o1); +Assert.isObject(f); +Assert.equal(JSON.stringify(f), '{"n":5,"s":"s","str":"a"}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..27fa8ad98bc6305971bbb54436f8a4f3cd78b11e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.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: > + Non-generic spread expressions continue to be processed as before: Call and construct signatures are stripped, + only non-method properties are preserved, and for properties with the same name, the type of the rightmost property is used. + This contrasts with intersection types which concatenate call and construct signatures, + preserve all properties, and intersect the types of properties with the same name. + Thus, spreads of the same types may produce different results when they are created through instantiation of generic types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func(arg1: T, arg2: U) { + return { ...arg1, ...arg2 }; +} + +let x: { mem1: string; mem2: { num: number } } = { mem1: "a", mem2: { num: 10 } }; +let y: { mem1: string; mem2: object } = { mem1: "b", mem2: { str: 'a' } }; + +let s1 = { ...x, ...y }; +Assert.isObject(s1); +let s2 = func(x, y); +Assert.isObject(s2); +let b1 = s1.mem2; +Assert.isObject(b1); +let b2 = s2.mem2; +b2 = { num: 20 }; +Assert.isObject(b2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts new file mode 100644 index 0000000000000000000000000000000000000000..550e160720172c31689abee065508db02dac4441 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts @@ -0,0 +1,73 @@ +/* + * 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: > + Common properties of unions are now considered discriminants as long as they contain some singleton type, + and they contain no generics. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" + +interface I{ + mem: { x: Error, y: undefined } | { x: undefined, y: T }; +} +function fun(arg: I) { + if (arg.mem.x) { + throw arg.mem.x; + } + return arg.mem.y; +} + +let i1 = { + mem: { x: undefined, y: undefined, } +}; +let result1 = fun(i1); +Assert.isUndefined(result1); + +let i2 = { + mem: { x: undefined, y: 10 } +}; +let result2 = fun(i2); +Assert.isNumber(result2); + +type R = { e: Error; data: null } | { e: null; data: T }; + +function func(result: R) { + if (result.e) { + throw result.e; + } + return result.data; +} + +var a = { + e: null, + data: null, +}; +var b = func(a); +let f = false; +if (b == null) { + f = true; +} +Assert.isTrue(f); + +var c = { + e: null, + data: 10, +}; +var d = func(c); +Assert.equal(d, 10); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.3/improved_behavior_for_calling_union_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.3/improved_behavior_for_calling_union_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..60bbc2c9648e978ded6c2f2f23bdba86f0df3322 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.3/improved_behavior_for_calling_union_types.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: > + Improved behavior for calling union types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type DamageType = "XO" | "EXP"; + type ColorType = "red" | "green" | "blue"; + + type ATK = (atk: DamageType) => number; + + let s: ATK = (atk: DamageType) => 1; + + type newColorConstructor = (color: ColorType) => string; + + let sp: newColorConstructor = (color: ColorType) => 'good'; + + Assert.equal(s("XO"), 1) + Assert.equal(sp("green"), 'good') + + interface Dog { + kind: "dog"; + dogProp: any; + } + + interface Cat { + kind: "cat"; + catProp: any; + } + + const petArray: Dog[] | Cat[] = [{ kind: "dog", dogProp: 1 }, { kind: "dog", dogProp: 2 }]; + let ex: number[] = []; + petArray.forEach((animal: Dog | Cat) => { + if (animal.kind === "dog") { + ex.push(animal.dogProp); + } else if (animal.kind === "cat") { + ex.push(animal.catProp); + } + }); + + Assert.equal(ex[0], 1); + Assert.equal(ex[1], 2); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Caveats.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Caveats.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f9ae0fabd600c625a50d3e798a1bf1e038d6948 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Caveats.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: > + One thing to note is that const assertions can only be applied immediately on simple literal expressions. + Another thing to keep in mind is that const contexts don’t immediately convert an expression to be fully immutable. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +let math = 1/3; +let compare = math<0.5? 1 as const:3 as const; +Assert.isNumber(compare); +Assert.equal(compare,1); + +let list = [9,8,7,6,5]; + +let bar = { + name: "foo", + age:1, + contents: list, +} as const; + +bar.contents.push(21); +Assert.equal(list[5],21); + +bar.contents.pop(); +Assert.equal(list.length,5); +Assert.equal(list[5],undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Type_checking_for_globalThis.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Type_checking_for_globalThis.ts new file mode 100644 index 0000000000000000000000000000000000000000..224330f3e1fc50012f3081571fe8d6e509103295 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/Type_checking_for_globalThis.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: > + globalThis provides a standard way for accessing the global scope which can be used across different environments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +var nm = 10010; +let gt:any = globalThis +gt.nm = 10086; +Assert.equal(gt.nm,10086); +Assert.equal(typeof gt.nm,"number"); + +var np = "sss"; +let gp:any = globalThis +gp.np = "nanj"; +Assert.equal(gp.np,"nanj"); +Assert.notEqual(typeof gp.np,"number"); +Assert.isString(gp.np); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/a_new_syntax_for_readonly_array.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/a_new_syntax_for_readonly_array.ts new file mode 100644 index 0000000000000000000000000000000000000000..078ba799592631a81757171fe5a4b58db2307c1e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/a_new_syntax_for_readonly_array.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: > + TypeScript 3.4 introduces a new syntax for ReadonlyArray using a new readonly modifier for array types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +let arr1: ReadonlyArray = ["1", "2"]; +let arr2: readonly string[] = ["1", "2"]; + +Assert.equal(arr1[1], "2"); +Assert.equal(arr2[1], "2"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/const_assertions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/const_assertions.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1d1900ab1beaa3663b873c46cf98346fef4db1e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/const_assertions.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: > + TypeScript 3.4 introduces a new construct for literal values called const assertions. Its syntax is a type assertion with const in place of the type name. + The angle bracket assertion syntax can also be used. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +let a1 = 1408 as const; +let a2 = 1408; +Assert.equal(a1, 1408); +Assert.equal(a2, 1408); +let b1 = 'NARC' as const; +let b2 = 'NARC'; +Assert.equal(b1, 'NARC'); +Assert.equal(b2, 'NARC'); +let c1 = [255, 0, 0] as const; +let c2 = [255, 0, 0]; +Assert.equal(JSON.stringify(c1), '[255,0,0]'); +Assert.equal(JSON.stringify(c2), '[255,0,0]'); +let d1 = { mem: 'member' } as const; +let d2 = { mem: 'member' }; +Assert.equal(JSON.stringify(d1), '{"mem":"member"}'); +Assert.equal(JSON.stringify(d2), '{"mem":"member"}'); + + +let obj1 = { kind: "circle", length: 80 }; +let obj2 = { kind: "square", length: 50 }; + +function func() { + let result = [obj1, obj2] as const; + return result; +} +for (const shape of func()) { + if (shape.kind === "circle") { + Assert.equal(shape.length, 80); + } else { + Assert.equal(shape.length, 50); + } +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/higher_order_type_inference.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/higher_order_type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..9fd47c8f43ff9252027790a35743ac5578118a5a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/higher_order_type_inference.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: > + Higher order type inference from generic functions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +function Func(f: (arg: A) => B, g: (arg: B) => C): (arg: A) => C { + return (x) => g(f(x)); +} + +interface A { + arr: T; +} + +function func1(index: T): T[] { + return [index]; +} + +function func2(arr: U): A { + return { arr }; +} + +const result = Func( + func1, + func2, +) + +Assert.equal(result("hello").arr[0].toUpperCase(), 'HELLO'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.4/readonly_tuples.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/readonly_tuples.ts new file mode 100644 index 0000000000000000000000000000000000000000..93d85342f50ae580991f7dc242d0e6483ae6ef2d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.4/readonly_tuples.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: > + TypeScript 3.4 introduces new support for readonly tuples. + a readonly tuple with elements 'T1, T2, … Tn' extends from ReadonlyArray'< T1 | T2 | … Tn >'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../suite/assert.js" + +let arr1: ReadonlyArray = [10, 'b', false]; +let arr2: readonly [number, string, boolean] = [5, 'a', true]; + +Assert.isNumber(arr1[0]); +Assert.isNumber(arr2[0]); +Assert.isString(arr1[1]); +Assert.isString(arr2[1]); +Assert.isBoolean(arr1[2]); +Assert.isBoolean(arr2[2]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.5/higher_order_type_inference.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/higher_order_type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..92578f067ccdbd5d6fea2914dbe7370d08f43d6f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/higher_order_type_inference.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: > + TypeScript 3.5 generalizes TypeScript 3.4's inference behavior to work on constructor functions as well. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + class A { + mem: T; + constructor(mem: T) { + this.mem = mem; + } + } + + class B { + mem: U; + constructor(mem: U) { + this.mem = mem; + } + } + + function func( + F: new (x: T) => U, + G: new (y: U) => V + ): (x: T) => V { + return x => new G(new F(x)); + } + + + let f = func(A, B); + + let a = f(10); + + Assert.equal(a.mem.mem, 10); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.5/improved_excess_property_checks_in_union_types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/improved_excess_property_checks_in_union_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..144e5d91df114e4b793e26739ce55f2b1be97622 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/improved_excess_property_checks_in_union_types.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: > + In TypeScript 3.5, the type-checker at least verifies that all the provided properties belong to some union member and have the appropriate type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +interface I{ + num: number; + mem2: string; +} +type T = { + num: number; + str: string; +} + +type ut = I | T; +let result: ut = { + mem2: 'member', + num: 10, + str: 'string' +} +Assert.isNumber(result.num); +Assert.isObject(result); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.5/omit_helper_type.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/omit_helper_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..5712d517e08f9a634bdbf0cb8c9ba3a923673aaf --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/omit_helper_type.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: > + TypeScript 3.5 introduces the new Omit helper type, which creates a new type with some properties dropped from the original. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type TA = { + a: string; + b: number; + c: string; + }; + type TB = Omit; + let s: TB = { + a: 'string', + b: 1, + } + let s1: TA = { + a: 'string', + b: 1, + c: 'string' + } + Assert.isFalse("c" in s) + Assert.isTrue("c" in s1); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.5/smarter_union_type_checking.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/smarter_union_type_checking.ts new file mode 100644 index 0000000000000000000000000000000000000000..03e60b42205d3c8b24f3c32fdde2a2bb00b2b224 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.5/smarter_union_type_checking.ts @@ -0,0 +1,64 @@ +/* + * 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 TypeScript 3.5, when assigning to types with discriminant properties like in T, + the language actually will go further and decompose types like S into a union of every possible inhabitant type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../suite/assert.js' + +{ + type newTest = { mem: string } | { mem: number }; + interface I1{ + num: number; + boo: newTest; + } + interface I2{ + num: number; + boo: { mem: string }; + } + interface I3{ + num: number; + boo: { mem: number }; + } + let i1: I1 = { + num: 10, + boo: { mem: 'member' } + } + let i2: I2 | I3 = { + num: 20, + boo: { mem: 15 } + } + + i1 = i2; + Assert.equal(i1, i2); + + type T1 = { mem1: number, mem2: boolean }; + type T2 = { mem1: number, mem2: true } | { mem1: number, mem2: false }; + let t1: T1 = { + mem1: 10, + mem2: true + } + let t2: T2 = { + mem1: 20, + mem2: false + } + t1 = t2; + Assert.equal(t1, t2); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..37a080fd4c9e37179c8db924adf424086894b885 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_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: TypeScript 3.6 contains better support for Unicode characters in identifiers when emitting to ES2015 and later targets. + options: + target:es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +const 𝓱𝓮 = "ok"; +Assert.equal(𝓱𝓮, "ok"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f25d224be1eb5deef06b64ca6de96636d026968c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.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: users can write getters and setters in ambient contexts in TypeScript 3.6 and later. +module: ESNext +isCurrent: true + ---*/ + + +import { Assert } from "../../../suite/assert.js" +import { C } from "./test.js" + +let obj: C = { m: 1 }; + +Assert.equal(1, obj.m); + +class mC { + mem: string; + constructor(mem: string) { + this.mem = mem; + } + get mC_mem() { + return this.mem; + } + set mC_mem(str:string) { + this.mem = str; + } +} +let eg = new mC('member'); +Assert.equal(eg.mC_mem, 'member'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8c3cbedea9a05bbfcaff917230df93032f1e350e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +declare class C { + get m(): number; + set m(value: number); +} +export { C }; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/improved_UX_Around_Promises/improved_UX_Around_Promises.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/improved_UX_Around_Promises/improved_UX_Around_Promises.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c7addaed1f600da4b165f979b038842cc43bd02 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/improved_UX_Around_Promises/improved_UX_Around_Promises.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: For example, it’s often very common to forget to .then() or await the contents of a Promise before passing + it to another function. TypeScript’s error messages are now specialized, and inform the user that perhaps they should + consider using the await keyword. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +let s = 1; +let t = 1; +function d1() { + s = 2 +} +let i = 0; +async function d2(this:any):Promise { + s = 3; + return s; +} +async function d3(this:any):Promise { + t = 3; + return t; +} + + + +async function d4() { + d1(); + Assert.equal(s, 2); + d2().then( + () => { + s = 5 + } + ); + Assert.equal(s, 3); + await d3().then( + () => { + t = 5 + } + ); + Assert.equal(t, 5); +} +d4().then(r => {}); + + + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/more_accurate_array_spread/more_accurate_array_spread_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/more_accurate_array_spread/more_accurate_array_spread_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..075f30c799211dc9666de1792332e8b1a923c727 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/more_accurate_array_spread/more_accurate_array_spread_1.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: More Accurate Array Spread. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +const t1 = { name: "John", age: 30 }; +const t2 = ["javascript", "typescript", "react"]; +const t3 = { ...t1, skills: t2 }; +Assert.equal("name" in t3, true); +Assert.equal("age" in t3, true); +Assert.equal("skills" in t3, true); +Assert.equal("isJob" in t3, false); + +let arr = [...Array(3)]; +Assert.equal(arr[0], undefined); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cdc9c54341c0381bfa7930089cf10f6f170f5f32 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_1.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: In TypeScript 3.6, the checker now knows that the correct type for iter.next().value + options: + lib:es2015 + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +function* func() { + yield 100; + yield "Finished!"; + return false; +} +let t1 = func(); +let t2 = t1.next(); +Assert.isNumber(t2.value); +t2 = t1.next(); +Assert.isString(t2.value); +t2 = t1.next(); +Assert.isBoolean(t2.value); +t2 = t1.next(); +Assert.equal(t2.done, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b13864663e3040fd165da34cca19b4144e2e2123 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_2.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: > + The Iterator type now allows users to specify the yielded type, the returned type, and the type that next can accept. + To allow differentiation between returned values and yielded values, + TypeScript 3.6 converts the IteratorResult type to a discriminated union type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +class C { + name: string; + age: number; + isJob: boolean; + constructor(name: string, age: number, isJob: boolean) { + this.name = name; + this.age = age; + this.isJob = isJob; + } +} + +let c1 = new C("caihua", 12, false); + +const keys = Object.keys(c1); +let nextIndex = 0; +let x: Iterator = { + next() { + return nextIndex < keys.length + ? { + value: keys[nextIndex++], + done: false, + } + : { value: 0, done: true }; + }, +}; + +let dd = x.next(); +Assert.isString(dd.value); +Assert.isBoolean(dd.done); +Assert.equal(dd.value, "name"); +Assert.equal(dd.done, false); +x.next(); +x.next(); + +dd = x.next(); +Assert.isNumber(dd.value); +Assert.isBoolean(dd.done); +Assert.equal(dd.value, 0); +Assert.equal(dd.done, true); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7422d03f1acb43cecde2a6660e6f25e4cfac118a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_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: the new Generator type is an Iterator that always has both the return and throw methods present, and is also iterable. + options: + lib:es2017 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function* func(): Generator { + let i = 0; + while (true) { + if (yield i++) { + break; + } + } + return "done!"; +} + +let t1 = func(); +let t2 = t1.next(); +while (!t2.done) { + Assert.equal(typeof t2.value, "number"); + t2 = t1.next(typeof t2.value == "number"); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c04f98e76984fbf2a2cef218d88c0ccbb4ec0c0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_4.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: > + To correctly represent the types that can be passed in to a generator from calls to next(), + TypeScript 3.6 also infers certain uses of yield within the body of a generator function. + options: + lib:es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function* func() { + let x: number = yield; + Assert.isNumber(x); +} +let x = func(); +x.next(); +x.next(1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..d2a21ffb7d6bb67463bc6f13da683066082edc36 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.6/stricter_generators/stricter_generators_5.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: > + To correctly represent the types that can be passed in to a generator from calls to next(), + TypeScript 3.6 also infers certain uses of yield within the body of a generator function. + options: + lib:es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function* func(): Generator { + let i = 0; + while (true) { + let cc = yield i++; + Assert.isBoolean(cc); + if (cc) { + break; + } + } + return "done!"; +} + +let t1 = func(); +let t2 = t1.next(); +while (!t2.done) { + t2 = t1.next(t2.value === 5); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/assertion_functions/assertion_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/assertion_functions/assertion_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb4269a3e46a8b8a9610d570fbe2ec686a2ccebd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/assertion_functions/assertion_functions_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: > + Assertions in JavaScript are often used to guard against improper types being passed in. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + + +function func(x: any, y: any) { + Assert.equal(typeof x, "number"); + Assert.equal(typeof y, "number"); + + return x * y; +} +Assert.equal(typeof func(6, 8), "number"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/better_support_for_never/better_support_for_never.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/better_support_for_never/better_support_for_never.ts new file mode 100644 index 0000000000000000000000000000000000000000..cad8262fc7afeb64514c2ef17a6b964aca44a7dc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/better_support_for_never/better_support_for_never.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: > + Better Support for never -Returning Functions + module: ESNext + isCurrent: true + ---*/ + +import {Assert} from "../../../suite/assert.js"; +function test() { + process.exit(1); +} +Assert.equal(test(), 'never') \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8368cd3d74e3a4b28c1752ba45849d0af031cbbd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_1.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: > + We can think of this feature - the ?? operator - as a way to "fall back" to a default value when dealing with null or undefined. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" +let nn: number | null; +nn = 1408; +let str: string = "NARC"; +let x = nn ?? str; + +nn = null; +let y = nn ?? str; + +Assert.equal(typeof x, "number"); +Assert.equal(typeof y, "string"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e9aa847415474bfe521ac87aa12c5ecc734ef77 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/nullish_coalescing/nullish_coalescing_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 ?? operator can replace uses of || when trying to use a default value. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +let n = 0; +function func() { + n = n ?? 0.5; + return n; +} + +Assert.equal(func(), 0); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5fc9d90f47a15507160de08cc6bb360107a57cd5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_1.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: > + The star of the show in optional chaining is the new ?. operator for optional property accesses. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +const obj1 = { + x: { + y: 'NARC' + } +}; + +let str = obj1?.x.y; + +Assert.equal(str, "NARC"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa7fa04c8cc1427d0ff80afd5773d5150c58320f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_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: > + You might find yourself using ?. to replace a lot of code that performs repetitive nullish checks using the && operator. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +const obj = { + x: { + y: 'e2' + } +}; + +if (obj?.x?.y) { + Assert.equal(obj?.x?.y, "e2", "true"); +} +if (obj && obj.x && obj.x.y) { + Assert.equal(obj && obj.x && obj.x.y, "e2", "true"); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..33526523eea91ccb7563869c78f37bda2b73e8b8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_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: > + One use of optional chain is optional element access, which acts similarly to optional property accesses, but allows us to access non-identifier properties (e.g. arbitrary strings, numbers). +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../../suite/assert.js" + +function func(arr?: T[]) { + return arr?.[0]; +} + +const arr: number[] = [1, 25, 8]; +Assert.equal(func(arr), 1, "true"); + +const arrStr: string[] = ["flower", "xian"]; +Assert.equal(func(arrStr), "flower", "true"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..5970e1f4c84aa3308ffb7f43b41c8fb3117e8d07 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_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: > + There's also optional call, which allows us to conditionally call expressions if they’re not null or undefined. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class C{ + mem: string; + constructor(mem: string) { + this.mem = mem; + } + func(mem: string) { + this.mem = mem; + return this.mem; + } +} +async function method(str: string): Promise { + return str; +} + +async function func(str: string, arg?: (str: string) => void): Promise{ + let c = new C('member'); + let para = c.func('member'); + let x = arg?.(para); + Assert.isUndefined(x); + const result: string = await method(str); + let y = arg?.(para); + Assert.isUndefined(y); + return result; +} +func('string').then(res => { + Assert.isObject(res); +}).catch(err => { +}); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..63facc273c208051e3596bebc2451c4c58a42d7a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/optional_chaining/optional_chaining_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: > + The "short-circuiting" behavior that optional chains have is limited property accesses, calls, element accesses - it doesn't expand any further out from these expressions. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +const obj = { + n1: { + n2: 10 + } +}; +function func(divisor: number) { + return divisor + 4; +} +let result = obj?.n1?.n2 / func(1); +Assert.equal(typeof result, "number"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/recursive_type_aliases/recursive_type_aliases_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/recursive_type_aliases/recursive_type_aliases_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..67b78880da97f73c4c7e648ae5226084efa96aa4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/recursive_type_aliases/recursive_type_aliases_1.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: > + That's exactly what TypeScript 3.7 introduces. At the "top level" of a type alias, TypeScript will defer resolving type arguments to permit these patterns. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +type JSONType = + | string + | number + | boolean + | null + | JsonObjectX + | JsonArray; + +interface JsonObjectX { + [property: string]: JSONType; +} + +interface JsonArray extends Array { } +const myJson: JSONType = ["a", 2, true, null, { + "Damage": 1024, + "DamageType": "XO", +}, []]; + +Assert.equal(typeof myJson, "object"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.7/ts_nocheck_in_typescript_files/ts_nocheck_in_typescript_files.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/ts_nocheck_in_typescript_files/ts_nocheck_in_typescript_files.ts new file mode 100644 index 0000000000000000000000000000000000000000..47db4dd102bd365723da8f89eefba94449a271cd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.7/ts_nocheck_in_typescript_files/ts_nocheck_in_typescript_files.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: > + TypeScript 3.7 allows us to add // @ts-nocheck comments to the top of TypeScript files to disable semantic checks. Historically this comment was only respected in JavaScript source files in the presence of checkJs, but we’ve expanded support to TypeScript files to make migrations easier for all users. + module: ESNext + isCurrent: true +---*/ + + +// @ts-nocheck + +import { Assert } from "../../../suite/assert.js"; + +function add(a, b) { + let s = a + b; + return s; +} +Assert.isTrue(isNaN(add())); +Assert.equal(add("NARC"), "NARCundefined"); +Assert.isTrue(isNaN(add(1408))); +Assert.equal(add(1, 2), 3); +Assert.equal(add('A', 'B'), "AB"); +Assert.equal(add(10, "false"), "10false"); +Assert.equal(add(10, true), 11); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/1_type_only_imports_and_export.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/1_type_only_imports_and_export.ts new file mode 100644 index 0000000000000000000000000000000000000000..8bafe780ee62fdcbf12f7eb85133facae2eb2747 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/1_type_only_imports_and_export.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: TypeScript 3.8 adds a new syntax for type-only imports and exports. +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../suite/assert.js"; +import type { T } from "./some_module.js"; + +export type { T }; + +let sth: T = { legs: 5 }; +Assert.equal(5, sth.legs); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..892d70d776d8c64894720a1cdae0ee80deaf31a3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.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: TypeScript 3.8 brings support for ECMAScript’s private fields + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class student { + #name: string; + constructor(name: string) { + this.#name = name; + } + + getName(): string { + return this.#name; + } +} + +let students = new student("NARC"); +Assert.equal("NARC", students.getName()); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb3b26cc3e7f3f6d848976f59e03e262f2a521d7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_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: With private fields, each field name is unique to the containing class. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class stu { + #x = 10; + getX() { + return this.#x; + } +} + +class Dtu extends stu { + #y = 20; + getY() { + return this.#y; + } +} + +let dt = new Dtu(); + +Assert.equal(10, dt.getX()); +Assert.equal(20, dt.getY()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..74b146fe495cab5d0d8281ccf984a1bb87fa228b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_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: Accessing a private field on any other type will result in a TypeError! + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../suite/assert.js' + +class stu { + #len: number; + constructor(len: number) { + this.#len = len; + } + compare(other: any) { + return this.#len === other.#len; + } +} + +const a = new stu(300); +const c = new stu(300); + + +Assert.isTrue(a.compare(c)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/3_which_should_i_use.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/3_which_should_i_use.ts new file mode 100644 index 0000000000000000000000000000000000000000..1cb4dec00e44d37ff08bdc233c95d87f8af4bc3e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/3_which_should_i_use.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 difference between private and # + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../suite/assert.js' + +class One { + private x = 10; +} + +Assert.equal(10, new One()["x"]); + +class Two { + #x: number; + constructor(x: number) { + this.#x = x; + } + getX(): number { + return this.#x; + } +} + +let three = new Two(5); +Assert.equal(three.getX(), 5); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/4_export_star_as_ns_syntax.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/4_export_star_as_ns_syntax.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e0cb9b79968027b034def87e2eb2b2dcb0020d2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/4_export_star_as_ns_syntax.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: ECMAScript 2020 recently added a new syntax to support this pattern! +module: ESNext +isCurrent: true +---*/ + + +import { Assert } from "../../suite/assert.js" +import { NUMX } from "./export_star.js" + +Assert.equal(1024, NUMX.x); +Assert.equal("STRING", NUMX.getX("STRING")); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/export_star.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/export_star.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c768b7b42317761f21ef0d3a766f1e5824e4647 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/export_star.ts @@ -0,0 +1,17 @@ +/* + * 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 * as NUMX from "./my_module.js"; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/my_module.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/my_module.ts new file mode 100644 index 0000000000000000000000000000000000000000..55187964d77251cbac670120c2479a6473233625 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/my_module.ts @@ -0,0 +1,18 @@ +/* + * 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 let x = 1024; +export function getX(x: string) { return x; }; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.8/some_module.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/some_module.ts new file mode 100644 index 0000000000000000000000000000000000000000..ffb0e823ceecba3eef57bc6200896268298da099 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.8/some_module.ts @@ -0,0 +1,19 @@ +/* + * 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. + */ + + +const cat: any = { legs: 4 }; + +export type T = typeof cat; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.9/Parsing_differences_in_optional_assertions/Parsing_differences_in_optional_assertions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/Parsing_differences_in_optional_assertions/Parsing_differences_in_optional_assertions.ts new file mode 100644 index 0000000000000000000000000000000000000000..6488e62012d29f396367d0608c7f75e51be19047 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/Parsing_differences_in_optional_assertions/Parsing_differences_in_optional_assertions.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: > + Parsing Differences in Optional Chaining and Non-Null Assertions + module: ESNext + isCurrent: true + ---*/ + +import {Assert} from "../../../suite/assert.js"; + +let s; +s = { a:1, b:{c:2}}; +Assert.equal(s?.a, 1); +Assert.equal((s.b!).c, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e490ea8519d7b9338a1caa5319cbc8359ca109c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.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: > + recrent versions of TypeScript (around 3.7) have had updates to the declarations of functions like Promise.all and Promise.race. + unfortunately, that introduced a few regressions, especially when mixing in values with null or undefined. + this issue has now been fixed. + options: + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +interface I1 { + func1(): number; +} + +interface I2 { + func2(): number; +} + +async function func( + arg1: Promise, + arg2: Promise +) { + let [t1, t2] = await Promise.all([arg1, arg2]); + return t1.func1(); +} + +let para1: Promise = new Promise(() => { return 10; }); +let para2: Promise = new Promise(() => { return 5; }); +func(para1, para2); +Assert.isObject(func(para1, para2)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/3.9/ts-expect-error_comments/ts-expect-error_comments.ts b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/ts-expect-error_comments/ts-expect-error_comments.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f4396aff6908d4a9b728f5fa6690945fb80f97f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/3.9/ts-expect-error_comments/ts-expect-error_comments.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: > + when a line is preceded by a // @ts-expect-error comment, TypeScript will suppress that error from being reported; but if there’s no error, TypeScript will report that // @ts-expect-error wasn’t necessary. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../suite/assert.js' + +function toASCIICode(s: string) { + s += ""; + let arr: number[] = []; + let char: string[] = s.split(''); + for (let i = 0; i < s.length; i++) { + arr[i] = char[i].charCodeAt(0); + } + return arr; +} +// @ts-expect-error +let json = JSON.stringify(toASCIICode(123)); + +Assert.equal(json, "[49,50,51]");