Skip to content

Commit 66feb68

Browse files
authored
feat(index.d.ts): add typescript type declarations (#16)
1 parent 2cba32f commit 66feb68

7 files changed

Lines changed: 423 additions & 103 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ jobs:
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- uses: actions/checkout@v2
15-
15+
1616
- uses: actions/setup-node@v1
1717
with:
1818
node-version: '12'
19-
19+
2020
- name: Get yarn cache directory path
2121
id: yarn-cache-dir-path
2222
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
23-
23+
2424
- name: Cache yarn packages
2525
uses: actions/cache@v2
2626
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
@@ -36,7 +36,7 @@ jobs:
3636
path: ~/.m2
3737
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
3838
restore-keys: ${{ runner.os }}-m2
39-
39+
4040
- name: Cache shadow-cljs
4141
uses: actions/cache@v2
4242
with:
@@ -47,7 +47,9 @@ jobs:
4747
- run: yarn install --frozen-lockfile
4848

4949
- run: yarn shadow-cljs release npm
50+
- run: yarn bundle-ts
5051
- run: yarn shadow-cljs compile test
51-
52+
5253
- run: node out/node-tests.js
5354
- run: yarn jest js/tests
55+
- run: yarn tsd

bundle-ts.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mkdir dist/types
2+
cp -r types/ dist/types/.

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
verbose: true,
3+
moduleFileExtensions: ["js", "jsx", "ts", "tsx"],
4+
transform: {
5+
'^.+\\.(ts|tsx)?$': 'ts-jest',
6+
"^.+\\.(js|jsx)$": "babel-jest",
7+
}
8+
};

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"private": false,
99
"scripts": {
1010
"dev": "shadow-cljs watch dev",
11-
"build": "rm -rf dist && shadow-cljs release npm",
12-
"build:dev": "rm -rf dist && shadow-cljs compile npm",
13-
"test:js": "yarn build && jest js/tests",
14-
"test:js:dev": "yarn build:dev && jest js/tests",
11+
"build": "rm -rf dist && shadow-cljs release npm && yarn bundle-ts",
12+
"build:dev": "rm -rf dist && shadow-cljs compile npm && yarn bundle-ts",
13+
"test:js": "yarn build && jest js/tests && yarn tsd",
14+
"test:js:dev": "yarn build:dev && jest js/tests && yarn tsd",
1515
"test:cljs": "shadow-cljs compile test && node out/node-tests.js",
1616
"test:cljs:watch": "shadow-cljs watch test-autorun",
1717
"test": "yarn test:cljs && yarn test:js",
@@ -20,7 +20,8 @@
2020
"semantic-release": "semantic-release",
2121
"_postinstall": "husky install",
2222
"prepublish": "pinst --disable",
23-
"postpublish": "pinst --enable"
23+
"postpublish": "pinst --enable",
24+
"bundle-ts": "./bundle-ts.sh"
2425
},
2526
"config": {
2627
"commitizen": {
@@ -32,7 +33,6 @@
3233
},
3334
"devDependencies": {
3435
"@babel/cli": "7.11.6",
35-
"@babel/core": "7.11.6",
3636
"@babel/plugin-transform-modules-commonjs": "7.12.1",
3737
"@babel/plugin-transform-react-jsx": "7.10.4",
3838
"@commitlint/cli": "^11.0.0",
@@ -43,6 +43,7 @@
4343
"@semantic-release/github": "7.1.1",
4444
"@semantic-release/npm": "7.0.6",
4545
"@semantic-release/release-notes-generator": "9.0.1",
46+
"@types/react": "^16.9.56",
4647
"babel-runtime": "6.26.0",
4748
"commitizen": "^4.2.2",
4849
"create-react-class": "15.6.3",
@@ -57,10 +58,15 @@
5758
"react": "16.14.0",
5859
"react-component-benchmark": "0.0.4",
5960
"react-dom": "16.14.0",
61+
"react-test-renderer": "^16.14.0",
6062
"semantic-release": "17.2.2",
6163
"semantic-release-cli": "5.4.0",
62-
"shadow-cljs": "2.11.4"
64+
"shadow-cljs": "2.11.4",
65+
"ts-jest": "^26.4.4",
66+
"tsd": "^0.13.1",
67+
"typescript": "^4.0.5"
6368
},
69+
"types": "./dist/types",
6470
"keywords": [
6571
"react",
6672
"relational",
@@ -69,6 +75,9 @@
6975
"state",
7076
"graph"
7177
],
78+
"tsd": {
79+
"directory": "types/tests"
80+
},
7281
"repository": {
7382
"type": "git",
7483
"url": "https://github.com/homebaseio/homebase-react.git"

types/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Transaction = Array<object>;
2+
export type Schema = object;
3+
export type config = {schema?: Schema, initialData?: Transaction };
4+
export type Entity = {
5+
get: (...params:string[]) => any;
6+
}
7+
8+
export function HomebaseProvider(props: {config?:config, children:any}): any;
9+
export function useTransact(): [(transaction:Transaction) => any];
10+
export function useEntity(lookup: object | number): [Entity];
11+
export function useQuery(query: object, ...args: any): [Array<Entity>];

types/tests/index.test-d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {expectType, expectError} from 'tsd';
2+
import { HomebaseProvider, useTransact, useEntity, useQuery, Entity, Transaction} from '../index';
3+
4+
expectType<any>(HomebaseProvider({children: []}));
5+
expectError(HomebaseProvider("a"));
6+
expectError(HomebaseProvider(1));
7+
8+
expectType<[(transaction: Transaction) => any]>(useTransact());
9+
expectError(useTransact("blurb"));
10+
11+
expectType<[Entity]>(useEntity(1));
12+
expectError(useEntity());
13+
14+
expectType<[Entity[]]>(useQuery({}));
15+
expectError(useQuery("d"));

0 commit comments

Comments
 (0)