Skip to content

Commit 7b6cd17

Browse files
committed
fix: type declarations
Type declarations were not working in cjs because exports were types instead of classes.
1 parent 0f10077 commit 7b6cd17

File tree

10 files changed

+92
-4
lines changed

10 files changed

+92
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/**/package-lock.json

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"test:browser": "playwright-test test/test-*.cjs",
2424
"test:es": "mocha test/test-*.spec.js",
2525
"test:cjs": "npm run test:node && npm run test:browser",
26-
"test": "npm run test:es && npm run test:cjs"
26+
"test:types:ts": "npm test --prefix test/ts-use",
27+
"test:types:esm": "npm test --prefix test/esm-use",
28+
"test:types:cjs": "npm test --prefix test/cjs-use",
29+
"test:types": "npm run test:types:ts && npm run test:types:esm && npm run test:types:cjs",
30+
"test": "npm run test:es && npm run test:cjs && npm run test:types"
2731
},
2832
"license": "MIT",
2933
"author": "Irakli Gozalishvili <[email protected]>",

src/lib.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Encoder = TextEncoder
2-
type Decoder = TextDecoder
1+
declare var TextEncoder: typeof self.TextEncoder
2+
declare var TextDecoder: typeof self.TextDecoder
33

4-
export { Encoder as TextEncoder, Decoder as TextDecoder }
4+
export { TextEncoder, TextDecoder }

test/cjs-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "cjs-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit --allowJs --checkJs src/main.js"
9+
}
10+
}

test/cjs-use/src/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
const { TextEncoder, TextDecoder } = require("web-encoding")
4+
5+
/**
6+
* @param {string} text
7+
* @returns {Uint8Array}
8+
*/
9+
const encode = (text) =>
10+
new TextEncoder().encode(text)
11+
12+
/**
13+
* @param {Uint8Array} bytes
14+
* @returns {string}
15+
*/
16+
const decode = (bytes) =>
17+
new TextDecoder().decode(bytes)
18+
19+
module.exports = { encode, decode }

test/esm-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "esm-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit --allowJs --checkJs src/main.js"
9+
}
10+
}

test/esm-use/src/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-check
2+
3+
import { TextEncoder, TextDecoder } from "web-encoding"
4+
5+
/**
6+
* @param {string} text
7+
* @returns {Uint8Array}
8+
*/
9+
export const encode = (text) =>
10+
new TextEncoder().encode(text)
11+
12+
/**
13+
* @param {Uint8Array} bytes
14+
* @returns {string}
15+
*/
16+
export const decode = (bytes) =>
17+
new TextDecoder().decode(bytes)

test/ts-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ts-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit"
9+
}
10+
}

test/ts-use/src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TextEncoder, TextDecoder } from "web-encoding"
2+
3+
export const encode = (text:string):Uint8Array =>
4+
new TextEncoder().encode(text)
5+
6+
export const decode = (bytes:Uint8Array):string =>
7+
new TextDecoder().decode(bytes)

test/ts-use/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": true,
4+
"strict": true,
5+
"strictNullChecks": true,
6+
"strictFunctionTypes": true,
7+
"strictPropertyInitialization": true,
8+
"strictBindCallApply": true
9+
}
10+
}

0 commit comments

Comments
 (0)