• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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/*---
17desc: 05 Generics
18name: 05.generics/genericTypeArgumentWithDefaultParameterValue_06
19tags: []
20---*/
21
22class UserType {
23    name: string
24}
25
26class UserName {
27    name : string
28}
29
30interface I<T = UserType, T1 = UserName> {
31    foo(p: T) : T
32}
33
34class A : implements I {
35    data: T
36    data1: T1
37    constructor(p: T, p1: T1) {
38        this.data = p.type
39        this.data1 = p1.name
40    }
41
42    foo(p : T) : T {
43        return data;
44    }
45
46    bar(p : T1) : T1 {
47        return data1;
48    }
49}
50
51class B : extends A {}
52
53
54function main(): void {
55    let a1 = new A();
56    let a2 = new A<UserType, UserName>();
57    let a3 = new A<string, string>;
58
59    let b1 = new B();
60    let b2 = new B<UserType>();
61    let b3 = new B<string>;
62
63
64    let ra1 = a1.foo(new UserType(), new UserName());
65    let ra2 = a2.foo(new UserType(), new UserName());
66    let ra3 = a3.foo("hello", "world")
67
68    let ra11 = a1.bar(new UserType(), new UserName());
69    let ra21 = a2.bar(new UserType(), new UserName());
70    let ra31 = a3.bar("hello", "world")
71
72    assert ra1 instanceof UserType
73    assert ra2 instanceof UserType
74    assert ra3 instanceof string
75
76    assert ra11 instanceof UserName
77    assert ra21 instanceof UserName
78    assert ra31 instanceof string
79}
80