Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d157e1

Browse files
committed
完成小节:三斜线指令
1 parent c459f50 commit 8d157e1

File tree

28 files changed

+307
-63
lines changed

28 files changed

+307
-63
lines changed

basics/declaration-files.md

Lines changed: 177 additions & 21 deletions
Large diffs are not rendered by default.

examples/declaration-files/04-declare-const-jquery/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare const jQuery: (selector: string) => any;
1+
// src/index.ts
22

33
jQuery('#foo');
44
// 使用 declare const 定义的 jQuery 类型,禁止修改这个全局变量
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// src/jQuery.d.ts
2+
3+
declare const jQuery: (selector: string) => any;

examples/declaration-files/06-declare-function/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
declare function jQuery(selector: string): any;
2-
declare function jQuery(domReadyCallback: () => any): any;
1+
// src/index.ts
32

43
jQuery('#foo');
54
jQuery(function() {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// src/jQuery.d.ts
2+
3+
declare function jQuery(selector: string): any;
4+
declare function jQuery(domReadyCallback: () => any): any;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// src/Animal.d.ts
2+
3+
declare class Animal {
4+
name: string;
5+
constructor(name: string);
6+
sayHi(): string;
7+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
declare class Animal {
2-
name: string;
3-
constructor(name: string);
4-
sayHi() {
5-
return `My name is ${this.name}`;
6-
}
7-
// ERROR: An implementation cannot be declared in ambient contexts.
8-
}
1+
// src/index.ts
2+
3+
let cat = new Animal('Tom');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// src/Directions.d.ts
2+
3+
declare enum Directions {
4+
Up,
5+
Down,
6+
Left,
7+
Right
8+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
declare enum Directions {
2-
Up,
3-
Down,
4-
Left,
5-
Right
6-
}
1+
// src/index.ts
72

83
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];

examples/declaration-files/09-declare-namespace/src/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
declare namespace jQuery {
2-
function ajax(url: string, settings?: any): void;
3-
const version: number;
4-
class Event {
5-
blur(eventType: EventType): void;
6-
}
7-
enum EventType {
8-
CustomClick
9-
}
10-
}
1+
// src/index.ts
112

123
jQuery.ajax('/api/get_something');
134
console.log(jQuery.version);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// src/jQuery.d.ts
2+
3+
declare namespace jQuery {
4+
function ajax(url: string, settings?: any): void;
5+
const version: number;
6+
class Event {
7+
blur(eventType: EventType): void;
8+
}
9+
enum EventType {
10+
CustomClick
11+
}
12+
}

examples/declaration-files/10-declare-namespace-nesting/src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
declare namespace jQuery {
2-
function ajax(url: string, settings?: any): void;
3-
namespace fn {
4-
function extend(object: any): void;
5-
}
6-
}
1+
// src/index.ts
72

83
jQuery.ajax('/api/get_something');
94
jQuery.fn.extend({
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// src/jQuery.d.ts
2+
3+
declare namespace jQuery {
4+
function ajax(url: string, settings?: any): void;
5+
namespace fn {
6+
function extend(object: any): void;
7+
}
8+
}

examples/declaration-files/11-declare-namespace-dot/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
declare namespace jQuery.fn {
2-
function extend(object: any): void;
3-
}
1+
// src/index.ts
42

53
jQuery.fn.extend({
64
check: function() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// src/jQuery.d.ts
2+
3+
declare namespace jQuery.fn {
4+
function extend(object: any): void;
5+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
declare function jQuery(selector: string): any;
2-
declare namespace jQuery {
3-
function ajax(url: string, settings?: any): void;
4-
}
1+
// src/index.ts
52

63
jQuery('#foo');
74
jQuery.ajax('/api/get_something');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// src/jQuery.d.ts
2+
3+
declare function jQuery(selector: string): any;
4+
declare namespace jQuery {
5+
function ajax(url: string, settings?: any): void;
6+
}

examples/declaration-files/25-declare-global/package-lock.json renamed to examples/declaration-files/28-triple-slash-directives/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/declaration-files/25-declare-global/package.json renamed to examples/declaration-files/28-triple-slash-directives/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "25-declare-global",
2+
"name": "24-merge-global-namespace",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// src/index.ts
2+
3+
foo({});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"baseUrl": "./",
5+
"paths": {
6+
"*": ["types/*"]
7+
}
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// types/jquery-plugin/index.d.ts
2+
3+
/// <reference types="jquery" />
4+
5+
declare function foo(options: JQuery.AjaxSettings): string;

examples/declaration-files/29-triple-slash-directives-global/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "24-merge-global-namespace",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {},
12+
"devDependencies": {
13+
"@types/node": "^12.0.1"
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// src/index.ts
2+
3+
import { foo } from 'node-plugin';
4+
5+
foo(global.process);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"baseUrl": "./",
5+
"paths": {
6+
"*": ["types/*"]
7+
}
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// types/node-plugin/index.d.ts
2+
3+
/// <reference types="node" />
4+
5+
export function foo(p: NodeJS.Process): string;

0 commit comments

Comments
 (0)