1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15/**--- 16 description: > 17 typescript 2.3 adds support for declaring defaults for generic type parameters. 18 a class or interface declaration that merges with an existing class or interface declaration may introduce a default for an existing type parameter. 19 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. 20 module: ESNext 21 isCurrent: true 22 ---*/ 23 24 25import { Assert } from '../../../suite/assert.js' 26 27class CA<V, T, U>{ 28 ca0: V; 29 ca1: T; 30 ca2: U; 31 constructor(ca0: V, ca1: T, ca2: U) { this.ca0 = ca0; this.ca1 = ca1; this.ca2 = ca2; } 32 creatOBJ() { 33 let a = this.ca0; 34 let b = this.ca1; 35 let c = this.ca2; 36 return { a, b, c }; 37 } 38 toJSON() { 39 return JSON.stringify(this.creatOBJ()); 40 } 41} 42class CCA<V = number, T = V[], U = T[]> extends CA<V, T, U>{ } 43let cca1 = new CCA(0, [0, 0], "[0]"); 44Assert.equal(cca1.toJSON(), "{\"a\":0,\"b\":[0,0],\"c\":\"[0]\"}"); 45let cca2 = new CCA<string>("A", ["A"], [["A"], ["B"]]); 46Assert.equal(cca2.toJSON(), "{\"a\":\"A\",\"b\":[\"A\"],\"c\":[[\"A\"],[\"B\"]]}"); 47 48interface IB<V, T, U> { 49 ia: V; 50 ib: T; 51 ic: U; 52} 53interface CIB<V = number, T = V[], U = T[]> extends IB<V, T, U> { } 54let cib1: CIB = { ia: 0, ib: [0], ic: [[0], [1, 2]] }; 55Assert.equal(JSON.stringify(cib1), "{\"ia\":0,\"ib\":[0],\"ic\":[[0],[1,2]]}"); 56let cib2: CIB<string> = { ia: "A", ib: ["A", "B"], ic: [["A"], ["B", "C"]] }; 57Assert.equal(JSON.stringify(cib2), "{\"ia\":\"A\",\"ib\":[\"A\",\"B\"],\"ic\":[[\"A\"],[\"B\",\"C\"]]}"); 58