function getTotal(one : number, two : number) : number {
}
function sayHello() :void{ console.log("hello world") }
function errorFunction() : never { throw new Error() console.log("hello world") }
function add({one, two} : {one : number, two : number}) { return one + two }
const numberArr : number[] = [1, 2, 3] const stringArr : string[] = ['a', 'b', 'c'] const undefinedArr : undefined[] = [undefined] const arr : (number | string)[] = [1, 'string', 2] const objectArr : Lady[] = [ {name: '刘英', age: 18}, {name: '李智恩', age: 24} ]
type Lady = {name: string,age: number} class Madam { name: string, age: number } const objectArr : Madam[] = [ {name: '刘英', age: 18}, {name: '李智恩', age: 24} ]
const IU : [string, string, number][] = [ ['IU', 'singer', 28], ['IU', 'singer', 28], ['IU', 'singer', 28] ]
interface Girl { name: string; age: number; bust: number; waistline ?: number; //可选值 [prorname:string] : any; //随便写 say(): string //返回字符串 }
interface Singer extends Girl{ sing(): string }
class Singers implements Girl{ name = '刘英', age = 18, bust = 90, say(){ return "hello world" } } const girl = { name: 'IU', age: 24, bust: 94, say(){ return "hello world" }, sing(){ return "lalalala~~~" } }
const screenResume = (girl: Girl) => { girl.age < 25 && girl.bust > 90 && console.log(girl.name) girl.waistline && console.log(girl.name + '腰围是' + girl.waistline) } screenResume(girl)
class Lady{ content = 'Hello, world' sayHello() { return this.content } }
class IU extends Lasy { sayHello() { return super.sayHello() + 'Hi, honey' } sayLove() { return "I LOVE U" } }
const goddess = new IU() console.log(goddess.sayHello) console.log(goddess.sayLove)
// 类的内部和类的外部 class Person { public name: string; private age: number; // 唯一只能类的内部 protected bund: number; // 只能类的内部和继承 public sayHello() { console.log(this.name + 'say hello') } } const person = new Person() person.name = 'hangshao'
class Teacher extends Person { public sayBye() { this.bund } }
class Person { constructor(public name:string) {} } const person = new Person('iu') console.log(person.name)
class Teacher extends Person{ constructor(public age:number){ super('hang') } } const teacher = new Teacher(18) console.log(teacher.name) console.log(teacher.age)
class Lady { constructor(private _age:number) {} get age() { return this._age } set age(age : number) { this._age = age } }
const IU = new Lady(28) IU.age = 25 console.log(IU.age)
// static class Girl { static sayLove() { return 'I Love U' } } console.log(Girl.sayLove)
class Person { public readonly _name:string constructor(name: string){ this._name = name } } const person = new Person('hang') person._name = 'fjh' // 这不被允许
abstract class Girl{ abstract skill() }
class Waiter extends Girl{ skill() { console.log('fire!') } }
{ "include": ["demo.ts"], "exclude": ["demo2.ts"], "compilerOptions": { // 一般部署时开启,连接ts和js的映射 "sourceMap": true, "outDir": "./build", "rootDir": "./src", // ts严格 "strict": true, // 允许你的注解类型any不用特意表明,一般还是需要注解 "noImplicitAny": false, // 未使用的变量 "noUnusedLocals": true, } }
interface Waiter{ anjiao: boolean; say: ()=>{}; } interface Teacher{ anjiao: boolean; skill: ()=>{}; }
// 联合类型👇 function judgewho(animal: Waiter | Teacher) { if(animal.anjiao) { (animal as Teacher).skill() } else { (animal as Waiter).say() } } Ⅱ function judgewhos(animal : Waiter | Teacher){ if('skill' in animal){ animal.skill() } else { animal.say() } } Ⅲ typeof Ⅳ instanceof class NumberObj{ count: Number; } function addObj(first :object | NumberObj, second : object | NumberObj) { if(first instanceof NUmberObj) { return } return }
究极程序员必会 enum Status { MESSAGE, SPA, DABAOJIAN, } function getServe(status : any) { if (status === Status.MESSAGE) { return "message" } else if (status === Status.SPA) { return "spa" } } enum Status { MESSAGE = 1, SPA, DABAOJIAN, } Status[1]
<名字随便起一般用T>
function join<T, P>(first: T, second: P) {
return ${first}${second}
}
// 第一个参数传什么类型第二个参数必须一致
join<number, string>(1, "23")
function myFun(params: T[]){ return params } myFun(["132"])
class SelectGirl{ constructor(private girls: string[] | number[]) {} getGirl(index: number):string | number{ return this.girls[index] } } const selectGirl = new SelectGirl(["IU", "刘永", "小红"]) // 泛型重构 class SelectGirl { constructor(private girls: T[]) {} getGirl(index :number) : T { return this.girls[index] } } const selectGirl = new SelectGirl([1,2,3]) // 继续磨练 interface Girl{ name: string; } class SelectGirl { constructor(private girls: T[]) {} getGirl(index :number) : string { return this.girls[index].name; } } const selectGirl = new SelectGirl([ {name: "IU"}, {name: "IU"}, {name: "IU"}, ])
namespace Home { class Header{ constructor() { const elem = document.createElement("div") elem.innerText = "IU" doucument.body.appendChild(elem) } }
class Page{ constructor() { new Header() } } }
namespace Components{ export class Header { constructor() { const elem = document.createElement("div") elem.innerText = "IU" document.body.appendChild(elem) } } }
namespace Home{ export class Page{ constructor() { new Components.Header() } } }
"module": "amd" "outFile": "./build/page.js"
namespace Components { export namespace SubComponents{ export class Test{} } }
import { Header, Content, Footer } from "./components"
export default class Page{ constructor() { new Header() } }
require(["page"], function(page){ new page.default(); })
yarn add --dev parcel@next
-----------重构axios--------------------------
let x : [string, number] // 越界元素啥都报错 x[4] = 4
默认情况下null和undefined是所有类型的子类型。 就是说你可以把 null和undefined赋值给number类型的变量。 注意:我们鼓励尽可能地使用--strictNullChecks
let someValue: any = "this is a string"; let strLength: number = (someValue).length;
let someValue: any = "this is a string"; let strLength: number = (someValue as string).length;
对象展开还有其它一些意想不到的限制。 首先,它仅包含对象 自身的可枚举属性。 大体上是说当你展开一个对象实例时,你会丢失其方法:
class C {
p = 12;
m() {
}
}
let c = new C();
let clone = { ...c };
clone.p; // ok
clone.m(); // error!
interface Point { readonly x: number; readonly y: number; } TypeScript具有ReadonlyArray类型,它与Array相似,只是把所有可变方法去掉了,因此可以确保数组创建后再也不能被修改。 最简单判断该用readonly还是const的方法是看要把它做为变量使用还是做为一个属性。 做为变量使用的话用 const,若做为属性则使用readonly。、
还有最后一种跳过这些检查的方式,这可能会让你感到惊讶,它就是将这个对象赋值给一个另一个变量: 因为 squareOptions不会经过额外属性检查,所以编译器不会报错。
interface SearchFunc { (source: string, subString: string): boolean; }
class Animal { name: string; } class Dog extends Animal { breed: string; } // 错误:使用数值型的字符串索引,有时会得到完全不同的Animal! interface NotOkay { [x: number]: Animal; [x: string]: Dog; }
接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具有某些私有成员。
可选参数必须跟在必须参数后面。 我们也可以为参数提供一个默认值当用户没有传递这个参数或传递的值是undefined时
interface GenericIdentityFn { (arg: T): T; }
function identity(arg: T): T { return arg; }
let myIdentity: GenericIdentityFn = identity;
注意,无法创建泛型枚举和泛型命名空间。
我们在类那节说过,类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。
interface Lengthwise { length: number; }
function loggingIdentity(arg: T): T { console.log(arg.length); return arg; }
function extend<T, U>(first: T, second: U): T & U { let result = {} as T & U; for (let id in first) { result[id] = first[id] as any } for (let id in second) { if(!result.hasOwnproperty(id)) { result[id] = second[id] as any } } return result }
||||
"typename"必须是 "number", "string", "boolean"或 "symbol" typeof instanceof
使用了 --strictNullChecks,可选参数会被自动地加上 | undefined:
!去除了 null和 undefined:
type Easing = "ease-in" | "ease-out" | "ease-in-out";