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

Skip to content

Commit 43c1db3

Browse files
committed
完成声明文件中的模块插件
1 parent 20f686a commit 43c1db3

File tree

82 files changed

+708
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+708
-66
lines changed

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
node_modules
2+
13
*.md
24

35
_book
@@ -12,3 +14,5 @@ assets
1214

1315
.travis.yml
1416
package-lock.json
17+
18+
examples/declaration-files/19-export-default-enum-error/types/foo/index.d.ts

basics/declaration-files.md

Lines changed: 132 additions & 50 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare namespace jQuery {
2+
function ajax(url: string, settings?: any): void;
3+
namespace fn {
4+
function extend(object: any): void;
5+
}
6+
}
7+
8+
jQuery.ajax('/api/get_something');
9+
jQuery.fn.extend({
10+
check: function() {
11+
return this.each(function() {
12+
this.checked = true;
13+
});
14+
}
15+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare namespace jQuery.fn {
2+
function extend(object: any): void;
3+
}
4+
5+
jQuery.fn.extend({
6+
check: function() {
7+
return this.each(function() {
8+
this.checked = true;
9+
});
10+
}
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// src/index.ts
2+
3+
let settings: AjaxSettings = {
4+
method: 'POST',
5+
data: {
6+
name: 'foo'
7+
}
8+
};
9+
jQuery.ajax('/api/post_something', settings);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// src/jQuery.d.ts
2+
3+
interface AjaxSettings {
4+
method?: 'GET' | 'POST';
5+
data?: any;
6+
}
7+
declare namespace jQuery {
8+
function ajax(url: string, settings?: AjaxSettings): void;
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// src/index.ts
2+
3+
let settings: jQuery.AjaxSettings = {
4+
method: 'POST',
5+
data: {
6+
name: 'foo'
7+
}
8+
};
9+
jQuery.ajax('/api/post_something', settings);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// src/jQuery.d.ts
2+
3+
declare namespace jQuery {
4+
interface AjaxSettings {
5+
method?: 'GET' | 'POST';
6+
data?: any;
7+
}
8+
function ajax(url: string, settings?: AjaxSettings): void;
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare function jQuery(selector: string): any;
2+
declare namespace jQuery {
3+
function ajax(url: string, settings?: any): void;
4+
}
5+
6+
jQuery('#foo');
7+
jQuery.ajax('/api/get_something');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// src/index.ts
2+
3+
import { name, getName, Animal, Directions, Options } from 'foo';
4+
5+
console.log(name);
6+
let myName = getName();
7+
let cat = new Animal('Tom');
8+
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
9+
let options: Options = {
10+
data: {
11+
name: 'foo'
12+
}
13+
};
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// types/foo/index.d.ts
2+
3+
export const name: string;
4+
export function getName(): string;
5+
export class Animal {
6+
constructor(name: string);
7+
sayHi(): string;
8+
}
9+
export enum Directions {
10+
Up,
11+
Down,
12+
Left,
13+
Right
14+
}
15+
export interface Options {
16+
data: any;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// src/index.ts
2+
3+
import { name, getName, Animal, Directions, Options } from 'foo';
4+
5+
console.log(name);
6+
let myName = getName();
7+
let cat = new Animal('Tom');
8+
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
9+
let options: Options = {
10+
data: {
11+
name: 'foo'
12+
}
13+
};
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// types/foo/index.d.ts
2+
3+
declare const name: string;
4+
declare function getName(): string;
5+
declare class Animal {
6+
constructor(name: string);
7+
sayHi(): string;
8+
}
9+
declare enum Directions {
10+
Up,
11+
Down,
12+
Left,
13+
Right
14+
}
15+
interface Options {
16+
data: any;
17+
}
18+
19+
export { name, getName, Animal, Directions, Options };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// src/index.ts
2+
3+
import { foo } from 'foo';
4+
5+
console.log(foo.name);
6+
foo.bar.baz();
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// types/foo/index.d.ts
2+
3+
export namespace foo {
4+
const name: string;
5+
namespace bar {
6+
function baz(): string;
7+
}
8+
}
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 'foo';
4+
5+
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// types/foo/index.d.ts
2+
3+
export default function foo(): string;
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 'foo';
4+
5+
console.log(foo.Down);
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// types/foo/index.d.ts
2+
3+
export default enum Directions {
4+
// ERROR: Expression expected.
5+
Up,
6+
Down,
7+
Left,
8+
Right
9+
}

examples/declaration-files/2-declare-jquery/.eslintrc.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
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 'foo';
4+
5+
console.log(foo.Down);
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// types/foo/index.d.ts
2+
3+
export default Directions;
4+
5+
declare enum Directions {
6+
Up,
7+
Down,
8+
Left,
9+
Right
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// 整体导入
2+
import foo = require('foo');
3+
// 单个导入
4+
import bar = foo.bar;
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// types/foo/index.d.ts
2+
3+
export = foo;
4+
5+
declare function foo(): string;
6+
declare namespace foo {
7+
const bar: number;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// src/index.ts
2+
3+
foo();
4+
console.log(foo.bar);
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// types/foo/index.d.ts
2+
3+
export as namespace foo;
4+
export = foo;
5+
6+
declare function foo(): string;
7+
declare namespace foo {
8+
const bar: number;
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface String {
2+
prependHello(): string;
3+
}
4+
5+
'foo'.prependHello();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

examples/declaration-files/24-merge-global-namespace/package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
"jquery": "^3.4.1"
13+
},
14+
"devDependencies": {
15+
"@types/jquery": "^3.3.29"
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// src/index.ts
2+
3+
jQuery.foo({
4+
bar: ''
5+
});

0 commit comments

Comments
 (0)