Skip to content

Commit e562021

Browse files
Merge pull request DefinitelyTyped#21194 from chrismbarr/improve-file-saver
[file-saver] Improve typedefs
2 parents b18de3a + fae8a9c commit e562021

File tree

4 files changed

+50
-133
lines changed

4 files changed

+50
-133
lines changed

types/file-saver/file-saver-tests.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1-
2-
3-
import { saveAs as importedSaveAs } from "file-saver";
4-
function testImportedSaveAs() {
5-
var data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
6-
var filename: string = 'hello world.txt';
7-
var disableAutoBOM = true;
8-
9-
importedSaveAs(data, filename, disableAutoBOM);
10-
}
1+
import "file-saver";
112

123
/**
134
* @summary Test for "saveAs" function.
145
*/
156
function testSaveAs() {
16-
var data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
17-
var filename: string = 'hello world.txt';
18-
var disableAutoBOM = true;
7+
const data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
8+
const filename = 'hello world.txt';
9+
const disableAutoBOM = true;
1910

2011
saveAs(data, filename, disableAutoBOM);
2112
}
2213

2314
/**
24-
* @summary Test for "saveAs" function.
15+
* @summary Test for "saveAs" function on the window object.
16+
*/
17+
function testWindowSaveAs() {
18+
const data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
19+
const filename = 'hello world.txt';
20+
const disableAutoBOM = true;
21+
22+
window.saveAs(data, filename, disableAutoBOM);
23+
}
24+
25+
/**
26+
* @summary Test for "saveAs" function with the 3rd parameter omitted
27+
*/
28+
function testOptionalOneParamSaveAs() {
29+
const data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
30+
const filename = 'hello world.txt';
31+
saveAs(data, filename);
32+
}
33+
34+
/**
35+
* @summary Test for "saveAs" function with the 2nd and 3rd parameters omitted
2536
*/
26-
function testSaveAsFile() {
27-
const data = new File(["Hello, world!"], "hello world.txt" ,{type: "text/plain;charset=utf-8"});
28-
37+
function testOptionalTwoParamsSaveAs() {
38+
const data: Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
2939
saveAs(data);
3040
}

types/file-saver/index.d.ts

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
1-
// Type definitions for FileSaver.js
1+
// Type definitions for FileSaver.js 1.3
22
// Project: https://github.com/eligrey/FileSaver.js/
3-
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>, Daniel Roth <https://github.com/DaIgeb>
4-
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
3+
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>
4+
// Daniel Roth <https://github.com/DaIgeb>
5+
// Chris Barr <https://github.com/chrismbarr>
6+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/file-saver
57

6-
/**
7-
* @summary Interface for "saveAs" function.
8-
* @author Cyril Schumacher
9-
* @version 1.0
10-
*/
11-
interface FileSaver {
12-
(
13-
/**
14-
* @summary Data.
15-
* @type {Blob}
16-
*/
17-
data: Blob,
18-
19-
/**
20-
* @summary File name.
21-
* @type {DOMString}
22-
*/
23-
filename: string,
24-
25-
/**
26-
* @summary Disable Unicode text encoding hints or not.
27-
* @type {boolean}
28-
*/
29-
disableAutoBOM?: boolean
30-
): void
31-
32-
(
33-
/**
34-
* @summary File.
35-
* @type {File}
36-
*/
37-
data: File
38-
): void
8+
declare namespace FileSaver {
9+
/**
10+
* FileSaver.js implements the saveAs() FileSaver interface in browsers that do not natively support it.
11+
* @param data - The actual file data blob.
12+
* @param filename - The optional name of the file to be downloaded. If omitted, the name used in the file data will be used. If none is provided "download" will be used.
13+
* @param disableAutoBOM - Optional & defaults to `false`. Set to `true` if you don't want FileSaver.js to automatically provide Unicode text encoding hints
14+
*/
15+
function saveAs(data: Blob, filename?: string, disableAutoBOM?: boolean): void;
3916
}
4017

41-
declare var saveAs: FileSaver;
18+
declare global {
19+
const saveAs: typeof FileSaver.saveAs;
4220

43-
declare module "file-saver" {
44-
var fileSaver: { saveAs: typeof saveAs };
45-
export = fileSaver
21+
interface Window {
22+
saveAs: typeof FileSaver.saveAs;
23+
}
4624
}
25+
26+
export = FileSaver;

types/file-saver/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"noImplicitAny": true,
99
"noImplicitThis": true,
10-
"strictNullChecks": false,
10+
"strictNullChecks": true,
1111
"strictFunctionTypes": true,
1212
"baseUrl": "../",
1313
"typeRoots": [
@@ -21,4 +21,4 @@
2121
"index.d.ts",
2222
"file-saver-tests.ts"
2323
]
24-
}
24+
}

types/file-saver/tslint.json

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,6 @@
11
{
22
"extends": "dtslint/dt.json",
33
"rules": {
4-
"adjacent-overload-signatures": false,
5-
"array-type": false,
6-
"arrow-return-shorthand": false,
7-
"ban-types": false,
8-
"callable-types": false,
9-
"comment-format": false,
10-
"dt-header": false,
11-
"eofline": false,
12-
"export-just-namespace": false,
13-
"import-spacing": false,
14-
"interface-name": false,
15-
"interface-over-type-literal": false,
16-
"jsdoc-format": false,
17-
"max-line-length": false,
18-
"member-access": false,
19-
"new-parens": false,
20-
"no-any-union": false,
21-
"no-boolean-literal-compare": false,
22-
"no-conditional-assignment": false,
23-
"no-consecutive-blank-lines": false,
24-
"no-construct": false,
25-
"no-declare-current-package": false,
26-
"no-duplicate-imports": false,
27-
"no-duplicate-variable": false,
28-
"no-empty-interface": false,
29-
"no-for-in-array": false,
30-
"no-inferrable-types": false,
31-
"no-internal-module": false,
32-
"no-irregular-whitespace": false,
33-
"no-mergeable-namespace": false,
34-
"no-misused-new": false,
35-
"no-namespace": false,
36-
"no-object-literal-type-assertion": false,
37-
"no-padding": false,
38-
"no-redundant-jsdoc": false,
39-
"no-redundant-jsdoc-2": false,
40-
"no-redundant-undefined": false,
41-
"no-reference-import": false,
42-
"no-relative-import-in-test": false,
43-
"no-self-import": false,
44-
"no-single-declare-module": false,
45-
"no-string-throw": false,
46-
"no-unnecessary-callback-wrapper": false,
47-
"no-unnecessary-class": false,
48-
"no-unnecessary-generics": false,
49-
"no-unnecessary-qualifier": false,
50-
"no-unnecessary-type-assertion": false,
51-
"no-useless-files": false,
52-
"no-var-keyword": false,
53-
"no-var-requires": false,
54-
"no-void-expression": false,
55-
"no-trailing-whitespace": false,
56-
"object-literal-key-quotes": false,
57-
"object-literal-shorthand": false,
58-
"one-line": false,
59-
"one-variable-per-declaration": false,
60-
"only-arrow-functions": false,
61-
"prefer-conditional-expression": false,
62-
"prefer-const": false,
63-
"prefer-declare-function": false,
64-
"prefer-for-of": false,
65-
"prefer-method-signature": false,
66-
"prefer-template": false,
67-
"radix": false,
68-
"semicolon": false,
69-
"space-before-function-paren": false,
70-
"space-within-parens": false,
71-
"strict-export-declare-modifiers": false,
72-
"trim-file": false,
73-
"triple-equals": false,
74-
"typedef-whitespace": false,
75-
"unified-signatures": false,
76-
"void-return": false,
77-
"whitespace": false
4+
"export-just-namespace": false
785
}
796
}

0 commit comments

Comments
 (0)