diff --git a/.changeset/happy-socks-grab/changes.json b/.changeset/happy-socks-grab/changes.json new file mode 100644 index 000000000..9bc96efcd --- /dev/null +++ b/.changeset/happy-socks-grab/changes.json @@ -0,0 +1,52 @@ +{ + "releases": [ + { "name": "@emotion/hash", "type": "patch" }, + { "name": "@emotion/memoize", "type": "patch" }, + { "name": "@emotion/weak-memoize", "type": "patch" } + ], + "dependents": [ + { + "name": "babel-plugin-emotion", + "type": "patch", + "dependencies": [ + "@emotion/serialize", + "@emotion/hash", + "@emotion/memoize" + ] + }, + { + "name": "@emotion/cache", + "type": "patch", + "dependencies": ["@emotion/hash", "@emotion/weak-memoize"] + }, + { + "name": "@emotion/serialize", + "type": "patch", + "dependencies": ["@emotion/hash", "@emotion/memoize"] + }, + { + "name": "@emotion/is-prop-valid", + "type": "patch", + "dependencies": ["@emotion/memoize"] + }, + { + "name": "emotion-theming", + "type": "patch", + "dependencies": ["@emotion/weak-memoize"] + }, + { + "name": "@emotion/primitives", + "type": "patch", + "dependencies": [ + "babel-plugin-emotion", + "@emotion/is-prop-valid", + "emotion-theming" + ] + }, + { + "name": "@emotion/styled-base", + "type": "patch", + "dependencies": ["@emotion/serialize", "@emotion/is-prop-valid"] + } + ] +} diff --git a/.changeset/happy-socks-grab/changes.md b/.changeset/happy-socks-grab/changes.md new file mode 100644 index 000000000..730775aa7 --- /dev/null +++ b/.changeset/happy-socks-grab/changes.md @@ -0,0 +1 @@ +Add TS types to util packages - hash, memoize & weak-memoize diff --git a/packages/hash/package.json b/packages/hash/package.json index 5cbbdd273..422830a58 100644 --- a/packages/hash/package.json +++ b/packages/hash/package.json @@ -4,6 +4,7 @@ "description": "A MurmurHash2 implementation", "main": "dist/hash.cjs.js", "module": "dist/hash.esm.js", + "types": "types/index.d.ts", "license": "MIT", "repository": "https://github.com/emotion-js/emotion/tree/master/packages/hash", "publishConfig": { @@ -11,10 +12,17 @@ }, "files": [ "src", - "dist" + "dist", + "types" ], + "scripts": { + "test:typescript": "dtslint types" + }, + "devDependencies": { + "dtslint": "^0.3.0" + }, "browser": { "./dist/hash.cjs.js": "./dist/hash.browser.cjs.js", "./dist/hash.esm.js": "./dist/hash.browser.esm.js" } -} \ No newline at end of file +} diff --git a/packages/hash/types/index.d.ts b/packages/hash/types/index.d.ts new file mode 100644 index 000000000..872aa0bef --- /dev/null +++ b/packages/hash/types/index.d.ts @@ -0,0 +1 @@ +export default function murmurhash2_32_gc(str: string): string diff --git a/packages/hash/types/tests.ts b/packages/hash/types/tests.ts new file mode 100644 index 000000000..fe6444e8a --- /dev/null +++ b/packages/hash/types/tests.ts @@ -0,0 +1,15 @@ +import hash from '@emotion/hash' + +// $ExpectType string +hash('color: hotpink;') + +// $ExpectError +hash() +// $ExpectError +const hashed2: number = hash('color: hotpink;') +// $ExpectError +hash(42) +// $ExpectError +hash({}) +// $ExpectError +hash('color: hotpink;', 'background-color: #fff;') diff --git a/packages/hash/types/tsconfig.json b/packages/hash/types/tsconfig.json new file mode 100644 index 000000000..9ff7ed289 --- /dev/null +++ b/packages/hash/types/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "forceConsistentCasingInFileNames": true, + "lib": [ + "es6", + "dom" + ], + "module": "commonjs", + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "target": "es5", + "typeRoots": [ + "../" + ], + "types": [] + }, + "include": [ + "./*.ts", + "./*.tsx" + ] +} diff --git a/packages/hash/types/tslint.json b/packages/hash/types/tslint.json new file mode 100644 index 000000000..b9706ef70 --- /dev/null +++ b/packages/hash/types/tslint.json @@ -0,0 +1,25 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "array-type": [ + true, + "generic" + ], + "import-spacing": false, + "semicolon": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-rest-spread", + "check-type", + "check-typecast", + "check-type-operator", + "check-preblock" + ], + + "no-unnecessary-generics": false + } +} diff --git a/packages/memoize/package.json b/packages/memoize/package.json index 329dcdcda..8207ff58f 100644 --- a/packages/memoize/package.json +++ b/packages/memoize/package.json @@ -4,17 +4,25 @@ "description": "emotion's memoize utility", "main": "dist/memoize.cjs.js", "module": "dist/memoize.esm.js", + "types": "types/index.d.ts", "license": "MIT", "repository": "https://github.com/emotion-js/emotion/tree/master/packages/memoize", + "scripts": { + "test:typescript": "dtslint types" + }, "publishConfig": { "access": "public" }, + "devDependencies": { + "dtslint": "^0.3.0" + }, "files": [ "src", - "dist" + "dist", + "types" ], "browser": { "./dist/memoize.cjs.js": "./dist/memoize.browser.cjs.js", "./dist/memoize.esm.js": "./dist/memoize.browser.esm.js" } -} \ No newline at end of file +} diff --git a/packages/memoize/types/index.d.ts b/packages/memoize/types/index.d.ts new file mode 100644 index 000000000..23f3580f1 --- /dev/null +++ b/packages/memoize/types/index.d.ts @@ -0,0 +1,3 @@ +type Fn = (key: string) => T + +export default function memoize(fn: Fn): Fn diff --git a/packages/memoize/types/tests.ts b/packages/memoize/types/tests.ts new file mode 100644 index 000000000..81b32b8ba --- /dev/null +++ b/packages/memoize/types/tests.ts @@ -0,0 +1,7 @@ +import memoize from '@emotion/memoize' + +// $ExpectType string[] +memoize((arg: string) => [arg])('foo') + +// $ExpectError +memoize((arg: number) => [arg]) diff --git a/packages/memoize/types/tsconfig.json b/packages/memoize/types/tsconfig.json new file mode 100644 index 000000000..9ff7ed289 --- /dev/null +++ b/packages/memoize/types/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "forceConsistentCasingInFileNames": true, + "lib": [ + "es6", + "dom" + ], + "module": "commonjs", + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "target": "es5", + "typeRoots": [ + "../" + ], + "types": [] + }, + "include": [ + "./*.ts", + "./*.tsx" + ] +} diff --git a/packages/memoize/types/tslint.json b/packages/memoize/types/tslint.json new file mode 100644 index 000000000..b9706ef70 --- /dev/null +++ b/packages/memoize/types/tslint.json @@ -0,0 +1,25 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "array-type": [ + true, + "generic" + ], + "import-spacing": false, + "semicolon": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-rest-spread", + "check-type", + "check-typecast", + "check-type-operator", + "check-preblock" + ], + + "no-unnecessary-generics": false + } +} diff --git a/packages/weak-memoize/package.json b/packages/weak-memoize/package.json index 337b7e892..7533b5da6 100644 --- a/packages/weak-memoize/package.json +++ b/packages/weak-memoize/package.json @@ -4,17 +4,25 @@ "description": "A memoization function that uses a WeakMap", "main": "dist/weak-memoize.cjs.js", "module": "dist/weak-memoize.esm.js", + "types": "types/index.d.ts", "license": "MIT", "repository": "https://github.com/emotion-js/emotion/tree/master/packages/weak-memoize", + "scripts": { + "test:typescript": "dtslint types" + }, "publishConfig": { "access": "public" }, + "devDependencies": { + "dtslint": "^0.3.0" + }, "files": [ "src", - "dist" + "dist", + "types" ], "browser": { "./dist/weak-memoize.cjs.js": "./dist/weak-memoize.browser.cjs.js", "./dist/weak-memoize.esm.js": "./dist/weak-memoize.browser.esm.js" } -} \ No newline at end of file +} diff --git a/packages/weak-memoize/types/index.d.ts b/packages/weak-memoize/types/index.d.ts new file mode 100644 index 000000000..a2b9f0654 --- /dev/null +++ b/packages/weak-memoize/types/index.d.ts @@ -0,0 +1,5 @@ +type UnaryFn = (arg: Arg) => Return + +export default function weakMemoize( + func: UnaryFn +): UnaryFn diff --git a/packages/weak-memoize/types/tests.ts b/packages/weak-memoize/types/tests.ts new file mode 100644 index 000000000..72a869db9 --- /dev/null +++ b/packages/weak-memoize/types/tests.ts @@ -0,0 +1,36 @@ +import weakMemoize from '@emotion/weak-memoize' + +type Foo = { bar: 'xyz' } + +declare class Qwe { + answer: number +} + +// $ExpectType Array +weakMemoize((arg: Foo) => [arg])({ bar: 'xyz' }) + +// $ExpectType string[] +weakMemoize((arg: string) => [arg])('foo') + +// $ExpectError +weakMemoize((arg: Foo) => [arg])(42) + +// $ExpectError +weakMemoize((arg: string) => [arg]) +// $ExpectError +weakMemoize((arg: number) => [arg]) +// $ExpectError +weakMemoize((arg: boolean) => [arg]) +// $ExpectError +weakMemoize((arg: symbol) => [arg]) +// $ExpectError +weakMemoize((arg: void) => [arg]) +// $ExpectError +weakMemoize((arg: null) => [arg]) +// $ExpectError +weakMemoize((arg: undefined) => [arg]) + +weakMemoize((arg: () => void) => [arg]) +weakMemoize((arg: Map) => [arg]) +weakMemoize((arg: Set) => [arg]) +weakMemoize((arg: Qwe) => [arg]) diff --git a/packages/weak-memoize/types/tsconfig.json b/packages/weak-memoize/types/tsconfig.json new file mode 100644 index 000000000..9ff7ed289 --- /dev/null +++ b/packages/weak-memoize/types/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "baseUrl": "../", + "forceConsistentCasingInFileNames": true, + "lib": [ + "es6", + "dom" + ], + "module": "commonjs", + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "target": "es5", + "typeRoots": [ + "../" + ], + "types": [] + }, + "include": [ + "./*.ts", + "./*.tsx" + ] +} diff --git a/packages/weak-memoize/types/tslint.json b/packages/weak-memoize/types/tslint.json new file mode 100644 index 000000000..b9706ef70 --- /dev/null +++ b/packages/weak-memoize/types/tslint.json @@ -0,0 +1,25 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "array-type": [ + true, + "generic" + ], + "import-spacing": false, + "semicolon": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-rest-spread", + "check-type", + "check-typecast", + "check-type-operator", + "check-preblock" + ], + + "no-unnecessary-generics": false + } +}