Skip to content

Commit

Permalink
part2.01-03
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxiao committed Jul 31, 2019
1 parent a15cbee commit 0cca8b1
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/es6/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 单独导出
export let a = 1

// 批量导出
let b = 2
let c = 3
export { b, c }

// 导出接口
export interface P {
x: number;
y: number;
}

// 导出函数
export function f() {}

// 导出时起别名
function g() {}
export { g as G }

// 默认导出,无需函数名
export default function () {
console.log("I'm default")
}

// 引入外部模块,重新导出
export { str as hello } from './b'
2 changes: 2 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/es6/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 导出常量
export const str = 'Hello'
16 changes: 16 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/es6/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { a, b, c } from './a'; // 批量导入
import { P } from './a'; // 导入接口
import { f as F } from './a'; // 导入时起别名
import * as All from './a'; // 导入模块中的所有成员,绑定在 All 上
import myFunction from './a'; // 不加{},导入默认

console.log(a, b, c)

let p: P = {
x: 1,
y: 1
}

console.log(All)

myFunction()
4 changes: 4 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/es6/d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export = function () {
console.log("I'm default")
}
// export let a = 1
7 changes: 7 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/node/a.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = {
x: 1,
y: 2
}

// 整体导出
module.exports = a
5 changes: 5 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/node/b.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// exports === module.exports
// 导出多个变量
// module.exports = {}
exports.c = 3
exports.d = 4
11 changes: 11 additions & 0 deletions sourcecode/ts-base/src/part2.project/01.module/node/c.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let c1 = require('./a.node')
let c2 = require('./b.node')
let c3 = require('../es6/a')
import c4 = require('../es6/d')

console.log(c1)
console.log(c2)
// c3()
// console.log(c3)
// c3.default()
c4()
6 changes: 6 additions & 0 deletions sourcecode/ts-base/src/part2.project/02.namespace/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Shape {
const pi = Math.PI
export function cricle(r: number) {
return pi * r ** 2
}
}
12 changes: 12 additions & 0 deletions sourcecode/ts-base/src/part2.project/02.namespace/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="a.ts" />
namespace Shape {
export function square(x: number) {
return x * x
}
}

console.log(Shape.cricle(2))
console.log(Shape.square(2))

import cricle = Shape.cricle
console.log(cricle(2))
43 changes: 43 additions & 0 deletions sourcecode/ts-base/src/part2.project/03.merge/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
interface A {
x: number;
// y: string;
foo(bar: number): number; // 5
foo(bar: 'a'): string; // 2
}

interface A {
y: number;
foo(bar: string): string; // 3
foo(bar: string[]): string[]; // 4
foo(bar: 'b'): string; // 1
}

let a: A = {
x: 1,
y: 2,
foo(bar: any) {
return bar
}
}

class C {}
namespace C {
export let state = 1
}
console.log(C.state)

function Lib() {}
namespace Lib {
export let version = '1.0'
}
console.log(Lib.version)

enum Color {
Red,
Yellow,
Blue
}
namespace Color {
export function mix() {}
}
console.log(Color)

0 comments on commit 0cca8b1

Please sign in to comment.