当前位置:网站首页>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 全局变量
全局变量
foo
Contains 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
greet
The 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
函数, 还有一个属性numberOfGreetings
Indicates the number of welcomes so far.
let result = myLib.makeGreeting("hello, world");
console.log("The computed greeting is:" + result);
let count = myLib.numberOfGreetings;
声明:使用declare namespace
Describes 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
});
声明: 使用interface
Define 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
,一个返回string
function 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 组织类型
greeter
The 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
Greeter
object to create the greeting,或者继承Greeter
object 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;
}
边栏推荐
猜你喜欢
推荐系统_刘老师
mdk5.14无法烧录
3. Byte stream and character stream of IO stream
xss课堂内容复现
暴雨中的人
Five Minutes Introductory Text Processing Three Musketeers grep awk sed
Oreo domain name authorization verification system v1.0.6 public open source version website source code
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
After the tester with 10 years of service "naked resignation" from the big factory...
LayaBox---TypeScript---首次接触遇到的问题
随机推荐
dotnet 启动 JIT 多核心编译提升启动性能
proe和creo的区别有哪些
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
Web3安全风险令人生畏,应该如何应对?
Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership
三种方式设置特定设备UWP XAML view
visual studio 2015 warning MSB3246
模拟对抗之红队免杀开发实践
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
How to make good use of builder mode
Interviewer: How is the expired key in Redis deleted?
【2022牛客多校5 A题 Don‘t Starve】DP
暴雨中的人
Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!
1、File对象学习
【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
88.(cesium之家)cesium聚合图
项目难管理?先学会用好甘特图(内附操作方法及实用模板)
Zero-knowledge proof - zkSNARK proof system