- 浏览: 2542710 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
2018 TypeScript Update(1)Introduction Basic Grammar - Types and Interface
Check the Current ENV
>node --version && npm --version
v8.0.0
5.6.0
Install TypeScript
>npm install -g typescript
Check Version
>tsc --version
Version 2.7.2
Language Related
http://www.typescriptlang.org/docs/handbook/basic-types.html
Basic Types
Boolean
let isDone: boolean = false;
Number
let decimal: number = 6;
let binary: number = 0b1010;
String
let color: string = “blue”;
let fullName: string = `Carl Luo`;
let age: number = 36;
let sentence: string = `Hello, my name is ${ fullName }.
I will be ${ age + 1} years old next year.`;
${} and `` in string are really useful.
Array
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
Tuple
let x : [string, number];
x = [“hello”, 10];
// x[0] is “hello”
// x[1] is 10
Enum
enum Color { Red, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green = 2, Blue = 4}
Any
let notSure: any = 4;
let list: any[] = [1, true, “free"];
Void
function warnUser(): void {
alert(“warning");
}
Null and Undefined
null undefined
Never
unreachable end point
Force the Type
let someValue: any = “this is a string”;
let strLength: number = (<string>someValue).length;
let strLength: number = (someValue as string).length;
http://www.typescriptlang.org/docs/handbook/variable-declarations.html
Variable Declarations
let and const
for (var i = 0;i<10;i++){
setTimeout(function() { console.log(i);}, 100*i);
}
the output will be 10,10,10,10…
not 0,1,2,3,...
Interface
http://www.typescriptlang.org/docs/handbook/interfaces.html
interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue){
console.log(labelledObj.label);
}
let myObj = { size: 10, label: “size 10 object”};
printLabel(myObj);
Optional Properties
interface SquareConfig {
color?: string;
width?: number;
}
Readonly Properties
interface Point {
readonly x: number;
readonly y: number;
}
let p1: Point = { x: 10, y:20 };
p1.x = 5; //error!
let a: number[] = [1, 2, 3, 4];
let ro: ReadonlyArray<number> = a; //once assigned, you can not change
ro[0] = 12; //error
a = ro as number[];
readonly VS const
Variables use const whereas properties use readonly.
Excess Property Checks
let mySqure = createSquare( { width: 100, opacity: 0.5 } as SquareConfig );
This will require the format in SquareConfig
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}
Function Types
interface SearchFunc {
(source: string, subString: string): boolean;
}
Define the params and return value for Function
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string){
let result = source.search(subString);
return result > -1;
}
Indexable Types
interface StringArray {
[index: number]: string;
}
let myArray: StringArray;
myArray = [“Bob”, “Fred"];
let myStr: string = myArray[0];
Readonly in Indexable Types
interface ReadonlyStringArray{
readonly [index: number]: string;
}
let myArray: ReadonlyStringArray = [“Alice”, “Bob”];
myArray[2] = “Mallory”;
Class Types
interface ClockInterface {
currentType: Date;
setTime(d: Date);
}
class Clock implements ClockInterface {
currentTime: Date;
setTime(d: Date){
this.currentTime = d;
}
constructor(h: number, m: number) {}
}
Extending Interfaces
one interface can extend multiple interfaces.
Hybrid Types
It works, but a little complex to understand. One object can be function and object.
interface Counter {
(start: number):string;
interval: number;
reset(): void;
}
function getCounter(): Counter {
let counter = <Counter> function (start: number) {};
counter.interval = 123;
counter.reset = function () {};
return counter;
}
let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Interfaces Extending Classes
interface will inherits the members of the class, not the implementation
References:
http://sillycat.iteye.com/blog/2285319
http://sillycat.iteye.com/blog/2285543
Check the Current ENV
>node --version && npm --version
v8.0.0
5.6.0
Install TypeScript
>npm install -g typescript
Check Version
>tsc --version
Version 2.7.2
Language Related
http://www.typescriptlang.org/docs/handbook/basic-types.html
Basic Types
Boolean
let isDone: boolean = false;
Number
let decimal: number = 6;
let binary: number = 0b1010;
String
let color: string = “blue”;
let fullName: string = `Carl Luo`;
let age: number = 36;
let sentence: string = `Hello, my name is ${ fullName }.
I will be ${ age + 1} years old next year.`;
${} and `` in string are really useful.
Array
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
Tuple
let x : [string, number];
x = [“hello”, 10];
// x[0] is “hello”
// x[1] is 10
Enum
enum Color { Red, Green, Blue}
let c: Color = Color.Green;
enum Color {Red = 1, Green = 2, Blue = 4}
Any
let notSure: any = 4;
let list: any[] = [1, true, “free"];
Void
function warnUser(): void {
alert(“warning");
}
Null and Undefined
null undefined
Never
unreachable end point
Force the Type
let someValue: any = “this is a string”;
let strLength: number = (<string>someValue).length;
let strLength: number = (someValue as string).length;
http://www.typescriptlang.org/docs/handbook/variable-declarations.html
Variable Declarations
let and const
for (var i = 0;i<10;i++){
setTimeout(function() { console.log(i);}, 100*i);
}
the output will be 10,10,10,10…
not 0,1,2,3,...
Interface
http://www.typescriptlang.org/docs/handbook/interfaces.html
interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue){
console.log(labelledObj.label);
}
let myObj = { size: 10, label: “size 10 object”};
printLabel(myObj);
Optional Properties
interface SquareConfig {
color?: string;
width?: number;
}
Readonly Properties
interface Point {
readonly x: number;
readonly y: number;
}
let p1: Point = { x: 10, y:20 };
p1.x = 5; //error!
let a: number[] = [1, 2, 3, 4];
let ro: ReadonlyArray<number> = a; //once assigned, you can not change
ro[0] = 12; //error
a = ro as number[];
readonly VS const
Variables use const whereas properties use readonly.
Excess Property Checks
let mySqure = createSquare( { width: 100, opacity: 0.5 } as SquareConfig );
This will require the format in SquareConfig
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}
Function Types
interface SearchFunc {
(source: string, subString: string): boolean;
}
Define the params and return value for Function
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string){
let result = source.search(subString);
return result > -1;
}
Indexable Types
interface StringArray {
[index: number]: string;
}
let myArray: StringArray;
myArray = [“Bob”, “Fred"];
let myStr: string = myArray[0];
Readonly in Indexable Types
interface ReadonlyStringArray{
readonly [index: number]: string;
}
let myArray: ReadonlyStringArray = [“Alice”, “Bob”];
myArray[2] = “Mallory”;
Class Types
interface ClockInterface {
currentType: Date;
setTime(d: Date);
}
class Clock implements ClockInterface {
currentTime: Date;
setTime(d: Date){
this.currentTime = d;
}
constructor(h: number, m: number) {}
}
Extending Interfaces
one interface can extend multiple interfaces.
Hybrid Types
It works, but a little complex to understand. One object can be function and object.
interface Counter {
(start: number):string;
interval: number;
reset(): void;
}
function getCounter(): Counter {
let counter = <Counter> function (start: number) {};
counter.interval = 123;
counter.reset = function () {};
return counter;
}
let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Interfaces Extending Classes
interface will inherits the members of the class, not the implementation
References:
http://sillycat.iteye.com/blog/2285319
http://sillycat.iteye.com/blog/2285543
发表评论
-
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 236MongoDB 2019(3)Security and Aut ... -
Memory Leak in NodeJS
2018-12-20 06:26 728Memory Leak in NodeJS I have d ... -
Remote Desktop Client
2018-12-07 13:19 1188Remote Desktop Client There is ... -
MetaBase UI Console(2)Docker on MySQL
2018-11-29 06:58 940MetaBase UI Console(2)Docker on ... -
AWS Lambda and Serverless Timeout
2018-09-20 01:20 626AWS Lambda and Serverless Timeo ... -
2018 WebSocket(1)Introduction
2018-03-20 01:22 11052018 WebSocket(1)Introduction ... -
2018 TypeScript Update(3)Introduction Basic Grammar
2018-03-08 03:08 6022018 TypeScript Update(3)Introd ... -
2018 TypeScript Update(2)Introduction Basic Grammar - Classes and Functions
2018-03-06 05:32 5512018 TypeScript Update(2)Introd ... -
Charts and Console(6)Paging
2018-03-01 00:12 578Charts and Console(6)Paging Th ... -
Vue.JS(3)Google Login
2018-02-14 04:53 1308Vue.JS(3)Google Login I just p ... -
Vue.JS(2)Monitor Water Console - ChartJS and Axios
2018-02-14 03:17 719Vue.JS(2)Monitor Water Console ... -
Vue.JS(1)Introduction and Basic Demo
2018-02-08 06:47 607Vue.JS(1)Introduction and Basic ... -
Charts and Console(5)Validation Form
2017-10-03 05:12 805Charts and Console(5)Validation ... -
Charts and Console(4)Display and Enhancement
2017-09-20 05:39 631Charts and Console(4)Display an ... -
Charts and Console(3)Auth and Login
2017-09-13 03:45 661Charts and Console(3)Auth and L ... -
Charts and Console(2)Login and Proxy
2017-08-31 05:39 877Charts and Console(2)Login and ... -
Charts and Console(1)UI Console and RESTful Client
2017-08-29 11:02 766Charts and Console(1)UI Console ... -
Blog Project(2)Express Backend API - istanbul - mocha - bunyan
2017-06-09 00:05 474Blog Project(2)Express Backend ... -
ReactJS(5)Composition vs Inheritance
2017-06-06 05:55 1111ReactJS(5)Composition vs Inheri ... -
ReactJS(4)Forms Lifting State Up
2017-06-06 04:34 1015ReactJS(4)Forms Lifting State U ...
相关推荐
knowleage01-typescript -- angularcli --- angu
interface User extends Name {// interface extends typeinterface User extends Nam
TypeScript Quickly-2020-英文版TypeScript Quickly-2020-英文版TypeScript Quickly-2020-英文版TypeScript Quickly-2020-英文版TypeScript Quickly-2020-英文版TypeScript Quickly-2020-英文版TypeScript Quickly-...
本文将深入探讨"vue-typescript-admin-template-master"这一项目,解析其核心知识点和实现原理。 1. Vue.js 框架基础 - 组件化:Vue.js的核心是组件系统,每个页面都可以看作是由多个可复用的组件拼接而成,提高了...
TypeScript
Vue3、Element_Plus、typescript后台管理系统_vue-manage-system
TypeScript Quickly-2020-英文版 学习笔记 TypeScript Quickly-2020-英文版 学习笔记 TypeScript Quickly-2020-英文版 学习笔记 TypeScript Quickly-2020-英文版 学习笔记 TypeScript Quickly-2020-英文版 学习笔记 ...
TypeScript
基于 Vue3.4、TypeScript、Vite5、Pinia、Element-Plus的一套后台管理框架源码+项目说明.zip基于 Vue3.4、TypeScript、Vite5、Pinia、Element-Plus的一套后台管理框架源码+项目说明.zip基于 Vue3.4、TypeScript、...
汇总插件类型script2 带有编译器错误的Typescript汇总插件。 这是对原始rollup-plugin-typescript的重写,从此开始并借用了。 这个版本比原始版本慢一些,但是它将打印出打字稿的句法和语义诊断消息(毕竟使用打字稿...
用法npm install --save-dev eslint@7 eslint-plugin-promise@4 eslint-plugin-import@2 eslint-plugin-node@11 @typescript-eslint/eslint-plugin@4 eslint-config-standard-with-typescript是的,这是很多软件包。...
基于 React18、React-Router V6、React-Hooks、Redux、TypeScript、Vite2、Ant-Design 开源的一套后台管理框架。
本源码为基于TypeScript的开源白板工具Excalidraw-mini设计,包含209个TSX文件、167个TS文件等,共619个文件。该项目旨在为用户提供一个全面、便捷的白板工具解决方案,通过TypeScript、JavaScript、HTML和CSS技术的...
详细介绍用EC6+构建Apps,使用React开发SPA应用,用create-react-app安装项目、Redux、React Router、TypeScript, Node用Express和GraphQL开发WEB服务等内容。
@ vue / eslint-config-typescript 用于vue-cli的eslint-config-typescript 有关可用规则,请参见 。 此配置是专为Vue CLI设置使用而设计的,并不供外部使用(可以使用,但可能需要在用户端进行一些修改-有关详细...
eslint-config-typescript-React 为TypeScript React厌烦ESLint Config 特征 标准配置 TypeScript配置 React配置 更漂亮的配置(消除了标准,TypeScript和React冲突) 安装 使用软件包管理器进行安装 yarn add @...
OpenAPI Typescript代码生成 根据OpenAPI规范生成Typescript客户端的Node.js库。...npm install openapi-typescript-codegen --save-dev 用法 $ openapi --help Usage: openapi [options] Options: -V, --version
json-to-ts-interface根据json字符串自动生成TypeScript interface定义使用方式:const interfaceDefinition = require('json-to-ts-interface');const res = interfaceDefinition(json对象||json字符串, {})参数...
Usage: swagger-typescript-api [options] Options: -v, --version output the current version -p, --path <path> path/url to swagger scheme -o, --output <output> output path of typescript api file (...