CDEC - Jay
function add(x: number, y: number): number {
return x + y;
}
function add(x: number, y: number, z?: number){
return x + y + (z || 0);
}
add(1, 2);
add(1, 2, 3);
add(1, 2, 3, 4); // Expected 2-3 arguments, but got 4.
function add(x: number, y: number, ...nums: number[]) {
return x + y + nums.reduce((current, item, arr) => { current += item; return current; }, 0);
}
add(1, 2, 3, 4, 5); // 15
type FunThisType = { a: number, b: string };
// 限定 this 的类型
function fun(this: FunThisType) {
return this.a;
}
function plus(a1: number, a2: number): number;
function plus(a1: string, a2: string): string;
function plus(a1, a2): any {
if (typeof a1 === 'number') {
return a1 + a2;
}
return a1 + a2;
}
plus(1, 2); // 3
plus(1, '2'); // Argument of type '1' is not assignable to parameter of type 'string'.
plus('1', '2'); // '12'
function fun<T>(a: T): string {
return typeof a;
}
// 函数变量使用泛型
const fun2: <T>(a: T) => string = a => {
return typeof a;
}
fun<number>(1);
fun('a');
fun<number>('2'); // Argument of type '"2"' is not assignable to parameter of type 'number'.
class Store<T>{
valueArr: T[];
push(val: T) {
this.valueArr.push();
}
pop() {
return this.valueArr.pop();
}
}
const numberStore = new Store<number>();
numberStore.push(1);
numberStore.push('1'); // Argument of type '"1"' is not assignable to parameter of type 'number'.
class Person {
name: string;
}
class Man extends Person {
}
function createUser<T extends Person>(user: T) {
return user;
}
createUser<Person>(Man);
createUser('abc'); // Argument of type '"abc"' is not assignable to parameter of type 'Person'.
function getProp<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
const obj = { a: 1, b: 'b' };
getProp(obj, 'a')
getProp(obj, 'c'); // Argument of type '"c"' is not assignable to parameter of type '"b" | "a"'.
// a.ts
export const a = 1;
export default 2;
// b.ts
import { a } from './a';
import defaultImport from './a';
// 重新导出
export const newExport = defaultImport;
// 别名导出
export { a as value } from './a';
// developer A
var a = 1;
function getA() {
return a;
}
// developer B(after 500 lines)
var a = 2;
// developer A
getA();
// developer A
var DA = {
a: 1,
getA(){
return this.a;
}
}
// developer B
var DB = {
a: 2
}
// developer A
DA.getA();
{
"compilerOptions": {
"module": "commonjs", // 生成的模块类型
"noImplicitAny": true, // 是否允许TypeScript 推断 any。
"strictNullChecks": true, // 强制 null 检查(null 值只能用于null类型)
"removeComments": true, // 移除注释
"sourceMap": true // 开启 SourceMap
},
"files": [
"hello.ts"
],
"include": [],
"exclude": []
}
注意:在命令行上指定的编译选项会覆盖在 tsconfig.json 文件里的相应选项。
// If true
function f1(a) { // Parameter 'a' implicitly has an 'any' type.
}
function f2(a: any) {
}
// If true
var a: void = null; // Type 'null' is not assignable to type 'void'.
var b: undefined = null; // Type 'null' is not assignable to type 'undefined'.
var c: null = null;
// If true
// 'this' implicitly has type 'any' because it does not have a type annotation.
function Greeter(greeting: string) {
this.greeting = greeting;
}
// If true
function fun(a: any) { // Not all code paths return a value.
if (a == 1) {
return 'abb';
}
}
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"jquery": ["node_modules/jquery/dist/jquery"] // 此处映射是相对于"baseUrl"
}
}
}
/// <reference types="node" />
import * as http from "http";
declare function createServer(): createServer.Server;
declare namespace createServer {
export type ServerHandle = HandleFunction | http.Server;
type NextFunction = (err?: any) => void;
export type SimpleHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse) => void;
export type NextHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type ErrorHandleFunction = (err: any, req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction;
export interface ServerStackItem {
route: string;
handle: ServerHandle;
}
export interface Server extends NodeJS.EventEmitter {
(req: http.IncomingMessage, res: http.ServerResponse, next?: Function): void;
route: string;
stack: ServerStackItem[];
use(fn: HandleFunction): Server;
use(route: string, fn: HandleFunction): Server;
handle(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void;
listen(port: number, hostname?: string, backlog?: number, callback?: Function): http.Server;
listen(port: number, hostname?: string, callback?: Function): http.Server;
listen(path: string, callback?: Function): http.Server;
listen(handle: any, listeningListener?: Function): http.Server;
}
}
export = createServer;
import a from 'ext-lib';
// global.d.ts
declare module 'ext-lib';
// 以 koa 为例
import * as koa from 'koa';
const app = new koa();
app.use((ctx: koa.Context) => {
ctx.request.abc;
ctx.request.ddd; // Property 'ddd' does not exist on type 'Request'.
});
declare module 'koa' {
export interface BaseRequest {
abc: string;
}
}