Skip to content

Commit cb8a583

Browse files
committed
add typescript check
1 parent e82b904 commit cb8a583

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

package-lock.json

Lines changed: 9 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
],
1616
"scripts": {
1717
"build": "rollup -c rollup.conf.js",
18-
"test": "npm run lint && npm i vue@2 --no-save && check-dts test/*.ts",
18+
"test": "npm run lint && npm run test:ts",
19+
"test:ts": "tsc -p .",
20+
"test:dts": "npm i vue@2 --no-save && check-dts test/*.ts",
1921
"lint": "eslint src/*.js",
2022
"lint:fix": "eslint src/*.js --fix",
2123
"prepublishOnly": "npm run test && npm run build",
@@ -49,6 +51,7 @@
4951
"eslint-config-airbnb-base": "^15.0.0",
5052
"eslint-plugin-import": "^2.27.5",
5153
"pre-commit": "^1.2.2",
52-
"rollup": "^3.13.0"
54+
"rollup": "^3.13.0",
55+
"typescript": "^4.9.5"
5356
}
5457
}

src/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @type Object{string: Promise<Element>} */
1+
/** @type Record<string, PromiseWithState<Element>> */
22
const cache = {};
33

44
/**
@@ -99,7 +99,7 @@ const InlineSvgComponent = {
9999
cache[src] = this.download(src);
100100
}
101101
// notify svg is unloaded
102-
if (this.svgElSource && cache[src].isPending() && !this.keepDuringLoading) {
102+
if (this.svgElSource && cache[src].getIsPending() && !this.keepDuringLoading) {
103103
this.svgElSource = null;
104104
this.$emit('unloaded');
105105
}
@@ -129,7 +129,7 @@ const InlineSvgComponent = {
129129
/**
130130
* Get the contents of the SVG
131131
* @param {string} url
132-
* @returns {Promise<Element>}
132+
* @returns {PromiseWithState<Element>}
133133
*/
134134
download(url) {
135135
return makePromiseState(new Promise((resolve, reject) => {
@@ -182,18 +182,20 @@ function setTitle(svg, title) {
182182
}
183183

184184
/**
185-
* @typedef {Promise} PromiseWithState
186-
* @property {Function<boolean>} isPending
185+
* @typedef {Promise & object} PromiseWithState
186+
* @property {function: boolean} getIsPending
187+
* @template T
187188
*/
188189

189190
/**
190191
* This function allow you to modify a JS Promise by adding some status properties.
191-
* @param {Promise|PromiseWithState} promise
192-
* @return {PromiseWithState}
192+
* @template {any} T
193+
* @param {Promise<T>|PromiseWithState<T>} promise
194+
* @return {PromiseWithState<T>}
193195
*/
194196
function makePromiseState(promise) {
195197
// Don't modify any promise that has been already modified.
196-
if (promise.isPending) return promise;
198+
if (promise.getIsPending) return promise;
197199

198200
// Set initial state
199201
let isPending = true;
@@ -210,7 +212,7 @@ function makePromiseState(promise) {
210212
},
211213
);
212214

213-
result.isPending = function getIsPending() { return isPending; };
215+
result.getIsPending = function getIsPending() { return isPending; };
214216
return result;
215217
}
216218

tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// from @https://typescript.nuxtjs.org/guide/setup/#configuration
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
"moduleResolution": "Node",
7+
"skipLibCheck": true,
8+
"lib": [
9+
"ESNext",
10+
"ESNext.AsyncIterable",
11+
"DOM"
12+
],
13+
"esModuleInterop": true,
14+
"allowJs": true,
15+
"checkJs": true,
16+
// "sourceMap": true,
17+
"strict": false,
18+
"noEmit": true,
19+
"resolveJsonModule": true,
20+
"baseUrl": ".",
21+
"paths": {
22+
"~/*": [
23+
"./*"
24+
],
25+
"@/*": [
26+
"./*"
27+
]
28+
},
29+
},
30+
"include": [
31+
"src/**/*",
32+
"test/**/*",
33+
"types/**/*",
34+
],
35+
}

0 commit comments

Comments
 (0)