mirror of
https://github.com/gnu4cn/ts-learnings.git
synced 2024-12-25 20:30:28 +08:00
Updated.
This commit is contained in:
parent
233d4feac0
commit
a3e7b3692d
1827
package-lock.json
generated
1827
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -25,9 +25,13 @@
|
||||
"@types/reflect-metadata": "^0.1.0",
|
||||
"@types/uuid": "^3.4.5",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-browserify": "^0.5.1",
|
||||
"gulp-clean": "^0.4.0",
|
||||
"gulp-concat": "^2.6.1",
|
||||
"gulp-sourcemap": "^1.0.1",
|
||||
"gulp-sourcemaps": "^2.6.1",
|
||||
"gulp-typescript": "^5.0.1",
|
||||
"gulp-uglify": "^3.0.0",
|
||||
"gulp-uglify": "^3.0.2",
|
||||
"live-server": "^1.2.0",
|
||||
"typescript": "^3.6.4"
|
||||
},
|
||||
|
8
src/Circle.ts
Normal file
8
src/Circle.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/// <reference path="IShape.ts" />
|
||||
namespace Drawing {
|
||||
export class Circle implements IShape {
|
||||
public draw(): void {
|
||||
console.log("Circle is drawn.");
|
||||
}
|
||||
}
|
||||
}
|
5
src/IShape.ts
Normal file
5
src/IShape.ts
Normal file
@ -0,0 +1,5 @@
|
||||
namespace Drawing {
|
||||
export interface IShape {
|
||||
draw(): void;
|
||||
}
|
||||
}
|
10
src/TestShape.ts
Normal file
10
src/TestShape.ts
Normal file
@ -0,0 +1,10 @@
|
||||
/// <reference path="IShape.ts" />
|
||||
/// <reference path="Circle.ts" />
|
||||
/// <reference path="Triangle.ts" />
|
||||
|
||||
function drawAllShapes(shape: Drawing.IShape) {
|
||||
shape.draw();
|
||||
}
|
||||
|
||||
drawAllShapes(new Drawing.Circle());
|
||||
drawAllShapes(new Drawing.Triangle());
|
8
src/Triangle.ts
Normal file
8
src/Triangle.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/// <reference path="IShape.ts" />
|
||||
namespace Drawing {
|
||||
export class Triangle implements IShape {
|
||||
public draw(): void{
|
||||
console.log("Triangle is drawn.");
|
||||
}
|
||||
}
|
||||
}
|
68
src/functions.ts
Normal file
68
src/functions.ts
Normal file
@ -0,0 +1,68 @@
|
||||
function push<T>(array: Array<T>, ...items: Array<T>) {
|
||||
items.forEach(i => {
|
||||
array.push(i);
|
||||
});
|
||||
}
|
||||
|
||||
let arrayA: Array<string> = [];
|
||||
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((<string>a).length){
|
||||
return (<string>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((<string>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(''));
|
Loading…
Reference in New Issue
Block a user