当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
漫画 | 老板裁掉我两周后,又把我请回去,工资翻番!
dotnet enables JIT multi-core compilation to improve startup performance
Spss-系统聚类软件实操
C#之app.config、exe.config和vshost.exe.config作用区别
该如何训练好深度学习模型?
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
visual studio 2015 warning MSB3246
无代码平台字段设置:基础设置入门教程
[Teach you to use the serial port idle interrupt of the STM32HAL library]
如何用好建造者模式
项目难管理?先学会用好甘特图(内附操作方法及实用模板)
ts集成和使用
宝塔实测-搭建中小型民宿酒店管理源码
[AGC] Build Service 1 - Cloud Function Example
Getting Started with Lattice Passwords
Big capital has begun to flee the crypto space?
动态规划_双数组字符串
如何进行AI业务诊断,快速识别降本提效增长点?
C语言之实现扫雷小游戏
ADB 安装 + 打驱动全教程