当前位置:网站首页>LayaBox---TypeScript---Example
LayaBox---TypeScript---Example
2022-08-04 21:08:00 【Gragra】
目录
1.介绍
The examples are in order of increasing complexity:
- 全局变量
- 全局函数
- Object with properties
- 函数重载
- Reusable types(接口)
- Reusable types(类型别名)
- 组织类型
- 类
2.例子
2.1 全局变量
全局变量
fooContains the total number of components present.
console.log("Half the number of widgets is " + (foo / 2)); 声明:使用declare var声明变量.
如果变量是只读的,那么可以使用 declare const. 你还可以使用 declare let如果变量拥有块级作用域.
/** 组件总数 */
declare var foo: number;2.2 全局函数
Called with a string argument
greetThe function displays a welcome message to the user.
greet("hello, world");声明: 使用declare function声明函数.
declare function greet(greeting: string): void;2.3 Object with properties
全局变量
myLib包含一个makeGreeting函数, 还有一个属性numberOfGreetingsIndicates the number of welcomes so far.
let result = myLib.makeGreeting("hello, world");
console.log("The computed greeting is:" + result);
let count = myLib.numberOfGreetings; 声明:使用declare namespaceDescribes a type or value accessed in dot notation.
declare namespace myLib {
function makeGreeting(s: string): string;
let numberOfGreetings: number;
}2.4. 函数重载
getWidget函数接收一个数字,返回一个组件,或接收一个字符串并返回一个组件数组.
let x: Widget = getWidget(43);
let arr: Widget[] = getWidget("all of them");声明:
declare function getWidget(n: number): Widget;
declare function getWidget(s: string): Widget[];2.5 Reusable types(接口)
When specifying a greeting,You must pass in one
GreetingSettings对象. This object has the following properties:1- greeting:Required string
2- duration: reliable duration(毫秒表示)
3- color: 可选字符串,比如‘#ff00ff’
greet({
greeting: "hello world",
duration: 4000
});声明: 使用interfaceDefine a type with properties.
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;2.6 Reusable types(类型别名)
Anywhere a welcome word is required,你可以提供一个
string,一个返回stringfunction or oneGreeter实例.
function getGreeting() {
return "howdy";
}
class MyGreeter extends Greeter { }
greet("hello");
greet(getGreeting);
greet(new MyGreeter());声明:Type aliases can be used to define short names for types:
type GreetingLike = string | (() => string) | MyGreeter;
declare function greet(g: GreetingLike): void;2.7 组织类型
greeterThe object can log to a file or display a warning. 你可以为.log(...)提供LogOptions和为.alert(...)提供选项.
const g = new Greeter("Hello");
g.log({ verbose: true });
g.alert({ modal: false, title: "Current Greeting" });声明: Use namespaces to organize types.
declare namespace GreetingLib {
interface LogOptions {
verbose?: boolean;
}
interface AlertOptions {
modal: boolean;
title?: string;
color?: string;
}
}You can also create nested namespaces within a declaration:
declare namespace GreetingLib.Options {
// Refer to via GreetingLib.Options.Log
interface Log {
verbose?: boolean;
}
interface Alert {
modal: boolean;
title?: string;
color?: string;
}
}2.8 类
You can do this by instantiating
Greeterobject to create the greeting,或者继承Greeterobject to customize the greeting.
const myGreeter = new Greeter("hello, world");
myGreeter.greeting = "howdy";
myGreeter.showGreeting();
class SpecialGreeter extends Greeter {
constructor() {
super("Very special greetings");
}
}声明: 使用declare class描述一个类或像类一样的对象. 类可以有属性和方法,就和构造函数一样.
declare class Greeter {
constructor(greeting: string);
greeting: string;
showGreeting(): void;
}边栏推荐
猜你喜欢

Web3时代的战争
![[2022 Nioke Duo School 5 A Question Don't Starve] DP](/img/fa/f1d11297cc5f58919bcc579f0a82e9.png)
[2022 Nioke Duo School 5 A Question Don't Starve] DP

Oreo domain name authorization verification system v1.0.6 public open source version website source code

零知识证明——zkSNARK证明体系
![[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path](/img/78/054329dec6a6faea5e9d583b6a8da5.png)
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path

Web3安全风险令人生畏,应该如何应对?

Matlab画图2

web漏洞扫描器-awvs

c语言小项目(三子棋游戏实现)

Interviewer: How is the expired key in Redis deleted?
随机推荐
Zynq Fpga图像处理之AXI接口应用——axi_lite接口使用
Debug locally and start the local server in vs code
括号匹配
Common methods of js's new Function()
After the tester with 10 years of service "naked resignation" from the big factory...
ADB 安装 + 打驱动全教程
命名路由、组件中name的作用
暴雨中的人
After encountering MapStruct, the conversion between PO, DTO and VO objects is no longer handwritten
[Data Mining] Written Exam Questions for Sohu Data Mining Engineers
基于单向链表结构的软件虚拟定时器的设计与构建
明明加了唯一索引,为什么还是产生了重复数据?
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
[TypeScript] In-depth study of TypeScript enumeration
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
如何进行AI业务诊断,快速识别降本提效增长点?
dotnet 使用 lz4net 压缩 Stream 或文件
路由中的meta、params传参的一些问题(可传不可传,为空,搭配,点击传递多次参数报错)
Getting Started with Lattice Passwords
Zero-knowledge proof - zkSNARK proof system