1.. 2 Copyright (c) 2021-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 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 13Modules 14======= 15 16| 17 18Programs are organized as sets of compilation units or modules. 19 20Each module creates its own scope, i.e., any declarations (variables, 21functions, classes, etc.) declared in the module are only visible outside 22that module if exported explicitly. 23 24Conversely, a variable, function, class, interface, etc. exported from 25another module must first be imported to a module. 26 27| 28 29Export 30------ 31 32A top-level declaration can be exported by using the keyword ``export``. 33 34A declared name that is not exported is considered private, and can be used 35only in the module it is declared in: 36 37.. code-block:: typescript 38 39 export class Point { 40 x: number = 0 41 y: number = 0 42 constructor(x: number, y: number) { 43 this.x = x 44 this.y = y 45 } 46 } 47 export let Origin = new Point(0, 0) 48 export function Distance(p1: Point, p2: Point): number { 49 return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)) 50 } 51 52| 53 54Import 55------ 56 57Import declarations are used to import entities exported from other modules, 58and to provide their bindings in the current module. An import declaration 59consists of the following two parts: 60 61* Import path that determines the module to import from; 62* Import bindings that define the set of usable entities in the imported 63 module, and the form of use (i.e., qualified or unqualified use). 64 65Import bindings can have several forms. 66 67If a module has the path '``./utils``', export entities 'X' and 'Y', and 68an import binding of the form '``* as A``' binds the name '``A``', then 69the qualified name ``A.name`` can be used to access all entities exported 70from the module defined by the import path: 71 72.. code-block:: typescript 73 74 import * as Utils from "./utils" 75 Utils.X // denotes X from Utils 76 Utils.Y // denotes Y from Utils 77 78An import binding of the form '``{ ident1, ..., identN }``' binds an exported 79entity with a specified name, which can be used as a simple name: 80 81.. code-block:: typescript 82 83 import { X, Y } from "./utils" 84 X // denotes X from Utils 85 Y // denotes Y from Utils 86 87If a list of identifiers contains aliasing of the form '``ident as alias``', 88then entity ``ident`` is bound under the name ``alias``: 89 90.. code-block:: typescript 91 92 import { X as Z, Y } from "./utils" 93 Z // denotes X from Utils 94 Y // denotes Y from Utils 95 X // Compile-time error: 'X' is not visible 96 97| 98 99Top-Level Statements 100--------------------- 101 102At the module level, a module can contain any statements, except ``return`` 103ones. 104 105If a module contains a ``main`` function (program entry point), then 106top-level statements of the module are executed immediately before 107the body of that function. Otherwise, such statements are executed 108before the execution of any other function of the module. 109 110| 111 112Program Entry Point 113-------------------- 114 115The top-level ``main`` function is an entry point of a program (application). 116The ``main`` function must have either an empty parameter list, or a single 117parameter of ``string[]`` type. 118 119.. code-block:: typescript 120 121 function main() { 122 console.log("this is the program entry") 123 } 124 125| 126| 127