• 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// override
17class Animal {
18    getName() {
19        return 'Animal';
20    }
21}
22class Dog extends Animal {
23    override getName() {
24        return 'Dog';
25    }
26}
27const dog = new Dog();
28console.log(dog.getName());
29
30// # private Class member
31class C {
32    #value: number = 0;
33    #add() {                               // private method
34      this.#value += 1;
35    }
36
37    static #message: string = 'hello';     // private static property
38    static #say() {
39      return this.#message;
40    }
41
42    get #data() {                          // private accessor
43      return this.#value;
44    }
45    set #data(num: number) {               // private accessor
46      this.#value = num;
47    }
48
49    static get #msg() {                    // private static accessor
50      return this.#message;
51    }
52
53    static set #msg(msg: string) {         // private static accessor
54      this.#message = msg;
55    }
56
57    publicMethod() {
58        C.#say();
59        this.#data = 1
60        C.#msg = 'Hello';
61        this.#add();
62        console.log(this.#data, this.#value, C.#msg);
63    }
64}
65let c: C = new C();
66c.publicMethod();
67
68// Static index signature
69class Foo {
70    hello = 'hello';
71    world = 1234;
72
73    [propName: string]: string | number | undefined;
74}
75let instance = new Foo();
76instance['whatever'] = 42;
77let x = instance['something'];
78
79// Symbol and template string index signature
80interface Colors {
81    [sym: symbol]: number;
82}
83
84const red = Symbol('red');
85const green = Symbol('green');
86const blue = Symbol('blue');
87
88let colors: Colors = {};
89colors[red] = 255;
90let redVal = colors[red];
91console.log(redVal);
92
93interface OptionsWithDataProps {
94    [optName: `option-${string}`]: string;
95}
96let idx: OptionsWithDataProps = {
97    'option-1': 'aaaa',
98    'option-2': 'bbbb',
99};
100console.log(idx['option-1']);
101
102
103// Static Block in class
104class StaticFoo {
105    static prop = 1
106    static {
107        console.log(StaticFoo.prop++);
108    }
109    static {
110        console.log(StaticFoo.prop++);
111    }
112    static {
113        console.log(StaticFoo.prop++);
114    }
115}
116console.log(StaticFoo.prop);
117
118
119// Instantiate expression
120function makeBox<T>(value: T) {
121    return { value };
122}
123const makeStringBox = makeBox<string>;
124console.log(makeStringBox('111'));
125
126// Satisfies operator
127type S = {
128    a: string,
129    b: string,
130}
131const s = {
132    a: 'aaa',
133    b: 'bbb',
134    c: 'ccc'
135}
136const isSatisfies = s satisfies S;
137
138// Class auto accessor
139class D {
140    accessor name: string;
141    constructor(name:string) {
142        this.name = name;
143    }
144}
145