-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liangxiao
committed
Jul 24, 2019
1 parent
dbe3cef
commit 4528459
Showing
16 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
《TypeScript 开发实战》课程资源 | ||
=============== | ||
|
||
* sourcode:源代码 | ||
* mindmap:思维导图 | ||
* ppt:课件 | ||
* mindmap:思维导图 | ||
* sourcode:源代码 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
}, | ||
"extends": [ | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-inferrable-types": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
基础篇、工程篇代码 | ||
=============== | ||
|
||
注意:src 中为独立的代码片段,多个文件之间会有变量重名。如果要运行其中的代码,请复制到 index.ts 中,或用 import 导入。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let a = 1; | ||
let b = [1, null, 'a'] | ||
let c = {x: 1, y: 'a'} | ||
|
||
let d = (x = 1) => x + 1 | ||
|
||
window.onkeydown = (event) => { | ||
// console.log(event.button) | ||
} | ||
|
||
interface Foo { | ||
bar: number | ||
} | ||
// let foo = {} as Foo | ||
// let foo = <Foo>{} | ||
let foo: Foo = { | ||
bar: 1 | ||
} | ||
// foo.bar = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* X(目标类型) = Y(源类型),X 兼容 Y | ||
*/ | ||
|
||
let s: string = 'a' | ||
// str = null | ||
|
||
// 接口兼容性 | ||
interface X { | ||
a: any; | ||
b: any; | ||
} | ||
interface Y { | ||
a: any; | ||
b: any; | ||
c: any; | ||
} | ||
let x: X = {a: 1, b: 2} | ||
let y: Y = {a: 1, b: 2, c: 3} | ||
x = y | ||
// y = x | ||
|
||
// 函数兼容性 | ||
type Handler = (a: number, b: number) => void | ||
function hof(handler: Handler) { | ||
return handler | ||
} | ||
|
||
// 1)参数个数 | ||
let handler1 = (a: number) => {} | ||
hof(handler1) | ||
let handler2 = (a: number, b: number, c: number) => {} | ||
// hof(handler2) | ||
|
||
// 可选参数和剩余参数 | ||
let a = (p1: number, p2: number) => {} | ||
let b = (p1?: number, p2?: number) => {} | ||
let c = (...args: number[]) => {} | ||
a = b | ||
a = c | ||
// b = a | ||
// b = c | ||
c = a | ||
c = b | ||
|
||
// 2)参数类型 | ||
let handler3 = (a: string) => {} | ||
// hof(handler3) | ||
|
||
interface Point3D { | ||
x: number; | ||
y: number; | ||
z: number; | ||
} | ||
interface Point2D { | ||
x: number; | ||
y: number; | ||
} | ||
let p3d = (point: Point3D) => {} | ||
let p2d = (point: Point2D) => {} | ||
p3d = p2d | ||
// p2d = p23 | ||
|
||
// 3) 返回值类型 | ||
let f = () => ({name: 'Alice'}) | ||
let g = () => ({name: 'Alice', location: 'Beijing'}) | ||
f = g | ||
// g = f | ||
|
||
// 函数重载 | ||
function overload(a: number, b: number): number | ||
function overload(a: string, b: string): string | ||
function overload(a: any, b: any): any {} | ||
// function overload(a: any): any {} | ||
// function overload(a: any, b: any, c: any): any {} | ||
// function overload(a: any, b: any) {} | ||
|
||
// 枚举兼容性 | ||
enum Fruit { Apple, Banana } | ||
enum Color { Red, Yellow } | ||
let fruit: Fruit.Apple = 1 | ||
let no: number = Fruit.Apple | ||
// let color: Color.Red = Fruit.Apple | ||
|
||
// 类兼容性 | ||
class A { | ||
constructor(p: number, q: number) {} | ||
id: number = 1 | ||
private name: string = '' | ||
} | ||
class B { | ||
static s = 1 | ||
constructor(p: number) {} | ||
id: number = 2 | ||
private name: string = '' | ||
} | ||
class C extends A {} | ||
let aa = new A(1, 2) | ||
let bb = new B(1) | ||
// aa = bb | ||
// bb = aa | ||
let cc = new C(1, 2) | ||
aa = cc | ||
cc = aa | ||
|
||
// 泛型兼容性 | ||
interface Empty<T> { | ||
// value: T | ||
} | ||
let obj1: Empty<number> = {}; | ||
let obj2: Empty<string> = {}; | ||
obj1 = obj2 | ||
|
||
let log1 = <T>(x: T): T => { | ||
console.log('x') | ||
return x | ||
} | ||
let log2 = <U>(y: U): U => { | ||
console.log('y') | ||
return y | ||
} | ||
log1 = log2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
enum Type { Strong, Week } | ||
|
||
class Java { | ||
helloJava() { | ||
console.log('Hello Java') | ||
} | ||
java: any | ||
} | ||
|
||
class JavaScript { | ||
helloJavaScript() { | ||
console.log('Hello JavaScript') | ||
} | ||
js: any | ||
} | ||
|
||
function isJava(lang: Java | JavaScript): lang is Java { | ||
return (lang as Java).helloJava !== undefined | ||
} | ||
|
||
function getLanguage(type: Type, x: string | number) { | ||
let lang = type === Type.Strong ? new Java() : new JavaScript(); | ||
|
||
if (isJava(lang)) { | ||
lang.helloJava(); | ||
} else { | ||
lang.helloJavaScript(); | ||
} | ||
|
||
// if ((lang as Java).helloJava) { | ||
// (lang as Java).helloJava(); | ||
// } else { | ||
// (lang as JavaScript).helloJavaScript(); | ||
// } | ||
|
||
// instanceof | ||
// if (lang instanceof Java) { | ||
// lang.helloJava() | ||
// // lang.helloJavaScript() | ||
// } else { | ||
// lang.helloJavaScript() | ||
// } | ||
|
||
// in | ||
// if ('java' in lang) { | ||
// lang.helloJava() | ||
// } else { | ||
// lang.helloJavaScript() | ||
// } | ||
|
||
// typeof | ||
// if (typeof x === 'string') { | ||
// console.log(x.length) | ||
// } else { | ||
// console.log(x.toFixed(2)) | ||
// } | ||
|
||
return lang; | ||
} | ||
|
||
getLanguage(Type.Week, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters