Skip to content

Commit

Permalink
Add TS types to util packages - hash, memoize, weak-memoize (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and emmatown committed Sep 12, 2019
1 parent 95b37bb commit c81c003
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 6 deletions.
52 changes: 52 additions & 0 deletions .changeset/happy-socks-grab/changes.json
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/happy-socks-grab/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add TS types to util packages - hash, memoize & weak-memoize
12 changes: 10 additions & 2 deletions packages/hash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
"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": {
"access": "public"
},
"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"
}
}
}
1 change: 1 addition & 0 deletions packages/hash/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function murmurhash2_32_gc(str: string): string
15 changes: 15 additions & 0 deletions packages/hash/types/tests.ts
Original file line number Diff line number Diff line change
@@ -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;')
26 changes: 26 additions & 0 deletions packages/hash/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
25 changes: 25 additions & 0 deletions packages/hash/types/tslint.json
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 10 additions & 2 deletions packages/memoize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
3 changes: 3 additions & 0 deletions packages/memoize/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Fn<T> = (key: string) => T

export default function memoize<T>(fn: Fn<T>): Fn<T>
7 changes: 7 additions & 0 deletions packages/memoize/types/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import memoize from '@emotion/memoize'

// $ExpectType string[]
memoize((arg: string) => [arg])('foo')

// $ExpectError
memoize((arg: number) => [arg])
26 changes: 26 additions & 0 deletions packages/memoize/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
25 changes: 25 additions & 0 deletions packages/memoize/types/tslint.json
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 10 additions & 2 deletions packages/weak-memoize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
5 changes: 5 additions & 0 deletions packages/weak-memoize/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type UnaryFn<Arg, Return> = (arg: Arg) => Return

export default function weakMemoize<Arg extends object, Return>(
func: UnaryFn<Arg, Return>
): UnaryFn<Arg, Return>
36 changes: 36 additions & 0 deletions packages/weak-memoize/types/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import weakMemoize from '@emotion/weak-memoize'

type Foo = { bar: 'xyz' }

declare class Qwe {
answer: number
}

// $ExpectType Array<Foo>
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<any, any>) => [arg])
weakMemoize((arg: Set<any>) => [arg])
weakMemoize((arg: Qwe) => [arg])
26 changes: 26 additions & 0 deletions packages/weak-memoize/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
25 changes: 25 additions & 0 deletions packages/weak-memoize/types/tslint.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit c81c003

Please sign in to comment.