diff --git a/gulpfile.js b/gulpfile.js index b97c6f8..d6a5cbb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -32,7 +32,8 @@ gulp.task("copy-html", () => { // 这里 watch 里必须使用 gulp.series gulp.task('watch', () => { - gulp.watch('./src/namespace/*.ts', gulp.series('clean', 'transpile-ts')); + // 这里监视所有src及其所有子文件夹下的ts文件 + gulp.watch('./src/**/*.ts', gulp.series('clean', 'copy-html', 'transpile-ts')); }); diff --git a/src/namespace/TestShape.ts b/src/App.ts similarity index 51% rename from src/namespace/TestShape.ts rename to src/App.ts index 47b43f8..3b7ad36 100644 --- a/src/namespace/TestShape.ts +++ b/src/App.ts @@ -1,6 +1,6 @@ -/// -/// -/// +/// +/// +/// function drawAllShapes(shape: Drawing.IShape) { shape.draw(); diff --git a/src/Circle.ts b/src/Circle.ts deleted file mode 100644 index 4cfb693..0000000 --- a/src/Circle.ts +++ /dev/null @@ -1,9 +0,0 @@ -/// - -namespace Drawing { - export class Circle implements IShape { - public draw(): void { - console.log("Circle is drawn."); - } - } -} diff --git a/src/accessor_decorators.ts b/src/accessor_decorators.ts deleted file mode 100644 index 3df7579..0000000 --- a/src/accessor_decorators.ts +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -// 访问器装饰器,Accessor Decorator -class Point { - private _x: number; - private _y: number; - constructor(x: number, y: number) { - this._x = x; - this._y = y; - } - - @configurable(false) - get x() { return this._x; } - - @configurable(false) - get y() { return this._y; } -} - -function configurable(value: boolean) { - return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { - descriptor.configurable = value; - }; -} diff --git a/src/array.ts b/src/array.ts deleted file mode 100644 index 2aa9828..0000000 --- a/src/array.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { v4 as uuid } from 'uuid'; - -export interface Person { - readonly id: string; - name: string; - gender: string; - [propName: string]: string|number; -} - -let user: Person = { - id: uuid(), - name: "彭海林", - gender: 'male', - age: 35 -} - -// 这里必须要对数组初始化,编译后的js才能运行 -let employees: Array = []; -employees.push(user); - -employees.forEach(p => {console.log(p)}); - -console.log(employees); -console.log(typeof(employees)); - -interface Persons { - [index: number]: Person; -} - -let employers: Persons = [user]; -console.log(employers); -console.log(typeof(employers)); - -function sum() { - let args: IArguments = arguments; -} - -let b: Boolean = new Boolean(1); -console.log(b, b.valueOf()); - -let e: Error = new Error("发生了错误。"); -console.log(typeof(e), e.message, e.name) - -let d: Date = new Date(); -console.log(d, typeof(d), d.toLocaleString()); - -let r: RegExp = /[a-z][A-Z]/ -console.log(r, typeof(r), r.test("This is a test")); - -console.log(Math.pow(10, 2)); -console.log(Math.E, Math.PI) - -let anyArray: Array = [5, 'Peng Hailin', {website: 'https:xfoss.com'}]; -console.log(anyArray, typeof anyArray); diff --git a/src/class.ts b/src/class.ts deleted file mode 100644 index 988b2a2..0000000 --- a/src/class.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { v4 as uuid } from 'uuid'; - -abstract class Thing { - protected id: string; - public constructor(protected name: string) { - this.name = name; - this.id = uuid(); - } - - getName() { - return this.name; - } - setName(name: string) { - this.name = name; - } - - public abstract sayHi(): string; -} - -class Animal extends Thing { - sayHi() { - return `Hi, I am ${this.name}, ${this.id} is my ID number.`; - } -} - -let cat = new Animal('Tom'); -console.log(cat.sayHi()); - - -interface Alarm { - alert(): void; -} - -interface Light { - lightOn(): void; - lightOff(): void; -} - -interface LightableAlarm extends Alarm { - lightOn(): void; - lightOff(): void; -} - -class Door { -} - -class SecurityDoor extends Door implements Alarm { - alert() { - console.log('SecurityDoor alert...'); - } -} - -class Car implements LightableAlarm { - alert() { - console.log('Car alert...'); - } - lightOn() { - console.log('The light is on.'); - } - lightOff() { - console.log('The light is off.'); - } -} - -let door = new SecurityDoor(); -console.log(door); -door.alert(); - -let car = new Car(); -console.log(car); -car.alert(); -car.lightOn(); -car.lightOff(); - -class Point { - x: number; - y: number; -} - -interface Point3D extends Point { - z: number; - calc(): string; -} - -class RealPoint implements Point3D { - public constructor(public x: number, public y: number, public z: number) { - this.x = x; - this.y = y; - this.z = z; - } - calc() { - let distance = Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2)); - return Math.round(distance).toString(); - } -} - -let pointA = new RealPoint(123, -19980, 10234); - -console.log(pointA, pointA.calc()); diff --git a/src/enum.ts b/src/enum.ts deleted file mode 100644 index 33cd087..0000000 --- a/src/enum.ts +++ /dev/null @@ -1,14 +0,0 @@ -enum Days {周日 = 7, 周一 = 1, 周二, 周三, 周四, 周五, 周六 = "周六".length} - -console.log(Days[2], Days["周二"]) - -const enum Directions { - Up, - Down, - Left, - Right -} - -let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right] - -console.log(directions) diff --git a/src/functions.ts b/src/functions.ts deleted file mode 100644 index 4403adf..0000000 --- a/src/functions.ts +++ /dev/null @@ -1,68 +0,0 @@ -function push(array: Array, ...items: Array) { - items.forEach(i => { - array.push(i); - }); -} - -let arrayA: Array = []; -push(arrayA, '1', '2', '3', '7'); -console.log(arrayA); -// [ 1, 2, 3, 'test' ] - -// 为了精准表达输入为数字,输出也是数字;输入为字符串,输出也是字符串 -function reverse(x: number): number; -function reverse(x: string): string; - -function reverse(x: number | string): number | string { - if (typeof x === 'number') { - return Number(x.toString().split('').reverse().join('')); - } else if (typeof x === 'string') { - return x.split('').reverse().join(''); - } -} - -console.log(reverse('This is a test.')); -console.log(reverse(2742312534)); - -interface SearchFunc { - (source: string, subString?: string): boolean; -} - -let mySearch: SearchFunc; -mySearch = function(source: string, subString: string = 'as') { - return source.search(subString) !== -1; -} -console.log(mySearch('This is a test.')); - -function buildName(firstName: string, lastName: string = 'Tom') { - return `${firstName} ${lastName}`; -} -console.log(buildName('John')); - -// Type Assertion -function getLength(a: string | number): number{ - if((a).length){ - return (a).length; - } else { - return a.toString().length; - } -} -console.log(getLength('This is a test.')); -console.log(getLength(988123035235)); - -function toBoolean(b: string | number): boolean { - if((b).length > 0) { - return true; - } else - if(Math.abs(b as number) > 0){ - return true; - } else return false; - - -} - -console.log(toBoolean(-1)); -console.log(toBoolean(0)); - -console.log(toBoolean('This is a test.')); -console.log(toBoolean('')); diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..efd89e2 --- /dev/null +++ b/src/index.html @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/interface.ts b/src/interface.ts deleted file mode 100644 index 4c4b510..0000000 --- a/src/interface.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { v4 as uuid } from 'uuid'; - -interface Person { - readonly id: string; - name: string; - gender: string; - [propName: string]: string|number; -} - -let user: Person = { - id: uuid(), - name: "彭海林", - gender: 'male', - age: 35 -} - -console.log(user.id); diff --git a/src/metadata.ts b/src/metadata.ts deleted file mode 100644 index 508a9a8..0000000 --- a/src/metadata.ts +++ /dev/null @@ -1,35 +0,0 @@ -import "reflect-metadata"; - -class Point { - x: number; - y: number; -} - -class Line { - private _p0: Point; - private _p1: Point; - - @validate - set p0(value: Point) { this._p0 = value; } - get p0() { return this._p0; } - - - @validate - set p1(value: Point) { this._p1 = value; } - get p1() { return this._p1; } -} - -function validate(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) { - let set = descriptor.set; - descriptor.set = function(value: T) { - let type = Reflect.getMetadata("design:type", target, propertyKey); - - if (!(value instanceof type)) { - throw new TypeError("无效的类型。"); - } - set(value); - } -} - -let l1 = new Line(); -console.log(l1); diff --git a/src/mixins_example.ts b/src/mixins_example.ts deleted file mode 100644 index 99d469c..0000000 --- a/src/mixins_example.ts +++ /dev/null @@ -1,56 +0,0 @@ -// 一个名为 Disposable 的混入 - -class Disposable { - isDisposable: boolean; - dispose () { - this.isDisposable = true; - } -} - -// 一个名为 Activatable 的混入 - -class Activatable { - isActive: boolean; - - activate() { - this.isActive = true; - } - - deactivate() { - this.isActive = false; - } -} - -class SmartObject implements Disposable, Activatable { - constructor() { - setInterval(() => { - console.log(`${this.isActive} : ${this.isDisposable}`) - }, 500); - } - - interact() { - this.activate(); - } - - // Disposable - isDisposable: boolean = false; - dispose: () => void; - - // Activatable - isActive: boolean = false; - activate: () => void; - deactivate: () => void; -} - -applyMixins(SmartObject, [Disposable, Activatable]); - -let smartObj = new SmartObject(); -setTimeout(() => smartObj.interact(), 1000); - -function applyMixins(derivedCtor: any, baseCtors: any[]) { - baseCtors.forEach(baseCtor => { - Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { - derivedCtor.prototype[name] = baseCtor.prototype[name]; - }); - }); -} diff --git a/src/parameter_decorators.ts b/src/parameter_decorators.ts deleted file mode 100644 index b5bc214..0000000 --- a/src/parameter_decorators.ts +++ /dev/null @@ -1,44 +0,0 @@ -import "reflect-metadata"; - -class Greeter { - greeting: string; - - constructor(message: string) { - this.greeting = message; - } - - @validate - greet(@required name: string) { - return "Hello " + name + ", " + this.greeting; - } -} - -let requiredMetadataKey: Symbol; - -function required(target: Object, propertyKey: string | symbol, parameterIndex: number) { - let existingRequiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyKey) || []; - - existingRequiredParameters.push(parameterIndex); - Reflect.defineMetadata(requiredMetadataKey, existingRequiredParameters, target, propertyKey); -} - -function validate (target: any, propertyName: string, descriptor: TypedPropertyDescriptor) { - let method = descriptor.value; - - descriptor.value = function () { - let requiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyName); - - if(requiredParameters) { - for ( let parameterIndex of requiredParameters ) { - if (parameterIndex >= arguments.length || arguments[parameterIndex] === undefined) { - throw new Error("缺少需要的参数。"); - } - } - } - - return method.apply(this, arguments); - } -} - -let g = new Greeter("早上好"); -console.log(g.greet("Echo Feng")); diff --git a/src/property_decorators.ts b/src/property_decorators.ts deleted file mode 100644 index 88d2168..0000000 --- a/src/property_decorators.ts +++ /dev/null @@ -1,29 +0,0 @@ -import "reflect-metadata"; - -// 属性装饰器,property decorator -class Greeter { - @format("Hello, %s") - greeting: string; - - constructor (message: string) { - this.greeting = message; - } - - greet () { - let formatString = getFormat(this, "greeting"); - return formatString.replace("%s", this.greeting); - } -} - -let formatMetadataKey: Symbol; - -function format (formatString: string) { - return Reflect.metadata(formatMetadataKey, formatString); -} - -function getFormat (target: any, propertyKey: string) { - return Reflect.getMetadata(formatMetadataKey, target, propertyKey); -} - -let g = new Greeter("彭海林"); -console.log(g.greet()); diff --git a/src/reference.lib.ts b/src/reference.lib.ts deleted file mode 100644 index 71d9e15..0000000 --- a/src/reference.lib.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// - -console.log("foo".padStart(4)); diff --git a/src/namespace/Circle.ts b/src/references/Circle.ts similarity index 100% rename from src/namespace/Circle.ts rename to src/references/Circle.ts diff --git a/src/namespace/IShape.ts b/src/references/IShape.ts similarity index 100% rename from src/namespace/IShape.ts rename to src/references/IShape.ts diff --git a/src/namespace/Triangle.ts b/src/references/Triangle.ts similarity index 99% rename from src/namespace/Triangle.ts rename to src/references/Triangle.ts index dac91fb..1173dc6 100644 --- a/src/namespace/Triangle.ts +++ b/src/references/Triangle.ts @@ -1,5 +1,4 @@ /// - namespace Drawing { export class Triangle implements IShape { public draw(): void{ diff --git a/src/symbol.ts b/src/symbol.ts deleted file mode 100644 index 503c877..0000000 --- a/src/symbol.ts +++ /dev/null @@ -1,2 +0,0 @@ -let a: Symbol; -console.log(a); diff --git a/src/tuple.ts b/src/tuple.ts deleted file mode 100644 index 85e34b8..0000000 --- a/src/tuple.ts +++ /dev/null @@ -1,2 +0,0 @@ -let x: [string, number]; -x[0] = 'John'; diff --git a/src/type-alias.ts b/src/type-alias.ts deleted file mode 100644 index 095ef36..0000000 --- a/src/type-alias.ts +++ /dev/null @@ -1,13 +0,0 @@ -type Name = string; -type NameResolver = () => string; -type NameOrResolver = Name | NameResolver; - -function getName(n: NameOrResolver): Name { - if ( typeof n === 'string' ) { - return n; - } else { - return n(); - } -} - -console.log(getName('Peng Hailin')); diff --git a/src/type-literals.ts b/src/type-literals.ts deleted file mode 100644 index 14cef63..0000000 --- a/src/type-literals.ts +++ /dev/null @@ -1,7 +0,0 @@ -type EventNames = 'click' | 'scroll' | 'mousemove'; -function handleEvent(ele: Element, event: EventNames) { - // do something -} - -handleEvent(document.getElementById('hello'), 'scroll'); -handleEvent(document.getElementById('hello'), 'mousemove'); diff --git a/tsconfig.json b/tsconfig.json index b2a8e30..db52846 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "include": [ - "src/namespace/*.ts" + "src/*.ts" ], "compilerOptions": { "noImplicitAny": true,