Skip to content

Commit 2a736d8

Browse files
committed
[typescript] test and fixes for index.d.ts
1 parent 74d02c8 commit 2a736d8

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ lib
99
es
1010
npm-debug.log
1111
.DS_Store
12+
.idea
13+
yarn.lock

package-scripts.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ module.exports = {
6464
description: 'flow check the entire project',
6565
script: 'flow check'
6666
},
67+
typescript: {
68+
description: 'typescript check the entire project',
69+
script: 'tsc'
70+
},
6771
validate: {
6872
description:
6973
'This runs several scripts to make sure things look good before committing or on clean install',
70-
default: concurrent.nps('lint', 'flow', 'build.andTest', 'test')
74+
default: concurrent.nps('lint', 'flow', 'typescript', 'build.andTest', 'test')
7175
}
7276
},
7377
options: {

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"jsnext:main": "dist/final-form-arrays.es.js",
77
"module": "dist/final-form-arrays.es.js",
88
"typings": "dist/index.d.js",
9-
"files": ["dist"],
9+
"files": [
10+
"dist"
11+
],
1012
"scripts": {
1113
"start": "nps",
1214
"test": "nps test",
1315
"precommit": "lint-staged && npm start validate"
1416
},
15-
"author":
16-
"Erik Rasmussen <[email protected]> (http://github.com/erikras)",
17+
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
1718
"license": "MIT",
1819
"repository": {
1920
"type": "git",
@@ -39,7 +40,7 @@
3940
"eslint-plugin-import": "^2.8.0",
4041
"eslint-plugin-jsx-a11y": "^6.0.3",
4142
"eslint-plugin-react": "^7.5.1",
42-
"final-form": "^1.3.5",
43+
"final-form": "^4.0.3",
4344
"flow": "^0.2.3",
4445
"flow-bin": "^0.61.0",
4546
"husky": "^0.14.3",
@@ -55,13 +56,17 @@
5556
"rollup-plugin-flow": "^1.1.1",
5657
"rollup-plugin-node-resolve": "^3.0.0",
5758
"rollup-plugin-replace": "^2.0.0",
58-
"rollup-plugin-uglify": "^2.0.1"
59+
"rollup-plugin-uglify": "^2.0.1",
60+
"typescript": "^2.6.2"
5961
},
6062
"peerDependencies": {
6163
"final-form": ">=1.2.0"
6264
},
6365
"lint-staged": {
64-
"*.{js*,ts,json,md,css}": ["prettier --write", "git add"]
66+
"*.{js*,ts*,json,md,css}": [
67+
"prettier --write",
68+
"git add"
69+
]
6570
},
6671
"bundlesize": [
6772
{

src/index.d.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// tslint:disable no-console
2+
3+
import { Config, createForm, AnyObject } from 'final-form'
4+
import * as arrayMutators from './index'
5+
import { Mutators } from './index'
6+
7+
const onSubmit: Config['onSubmit'] = (values, callback) => {}
8+
9+
const form = createForm({
10+
mutators: { ...arrayMutators },
11+
onSubmit
12+
})
13+
14+
// Get form.mutators (default as object) and cast to Mutators
15+
const mutators: Mutators = ((form.mutators || {}) as any) as Mutators
16+
17+
mutators.insert('customers', 0, { firstName: '', lastName: '' })
18+
mutators.move('customers', 0, 1)
19+
const customer = mutators.pop('customers')
20+
mutators.push('customers', { firstName: '', lastName: '' })
21+
const removed = mutators.remove('customers', 0)
22+
const shifted = mutators.shift('customers')
23+
mutators.swap('customers', 0, 1)
24+
mutators.unshift('customers', { firstName: '', lastName: '' })

src/index.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Mutator } from 'final-form'
22

3-
type DefaultType = { [key: string]: Mutator }
4-
5-
export default DefaultType
3+
export const insert: Mutator
4+
export const move: Mutator
5+
export const pop: Mutator
6+
export const push: Mutator
7+
export const remove: Mutator
8+
export const shift: Mutator
9+
export const swap: Mutator
10+
export const unshift: Mutator
611

712
/** The shape of the mutators once final-form has bound them to state */
8-
export type Mutators = {
13+
export interface Mutators {
914
insert: (name: string, index: number, value: any) => void
1015
move: (name: string, from: number, to: number) => void
1116
pop: (name: string) => any

tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"noEmit": true,
5+
"strict": true
6+
},
7+
"include": ["./src/**/*"]
8+
}

0 commit comments

Comments
 (0)