Skip to content

typescript tests and fixes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ lib
es
npm-debug.log
.DS_Store
.idea
yarn.lock
6 changes: 5 additions & 1 deletion package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ module.exports = {
description: 'flow check the entire project',
script: 'flow check'
},
typescript: {
description: 'typescript check the entire project',
script: 'tsc'
},
validate: {
description:
'This runs several scripts to make sure things look good before committing or on clean install',
default: concurrent.nps('lint', 'flow', 'build.andTest', 'test')
default: concurrent.nps('lint', 'flow', 'typescript', 'build.andTest', 'test')
}
},
options: {
Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"main": "dist/final-form-arrays.cjs.js",
"jsnext:main": "dist/final-form-arrays.es.js",
"module": "dist/final-form-arrays.es.js",
"typings": "dist/index.d.js",
"files": ["dist"],
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"start": "nps",
"test": "nps test",
"precommit": "lint-staged && npm start validate"
},
"author":
"Erik Rasmussen <[email protected]> (http://github.com/erikras)",
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -39,7 +40,7 @@
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.5.1",
"final-form": "^1.3.5",
"final-form": "^4.0.3",
"flow": "^0.2.3",
"flow-bin": "^0.61.0",
"husky": "^0.14.3",
Expand All @@ -55,13 +56,17 @@
"rollup-plugin-flow": "^1.1.1",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-uglify": "^2.0.1"
"rollup-plugin-uglify": "^2.0.1",
"typescript": "^2.6.2"
},
"peerDependencies": {
"final-form": ">=1.2.0"
},
"lint-staged": {
"*.{js*,ts,json,md,css}": ["prettier --write", "git add"]
"*.{js*,ts*,json,md,css}": [
"prettier --write",
"git add"
]
},
"bundlesize": [
{
Expand Down
24 changes: 24 additions & 0 deletions src/index.d.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// tslint:disable no-console

import { Config, createForm, AnyObject } from 'final-form'
import * as arrayMutators from './index'
import { Mutators } from './index'

const onSubmit: Config['onSubmit'] = (values, callback) => {}

const form = createForm({
mutators: { ...arrayMutators },
onSubmit
})

// Get form.mutators (default as object) and cast to Mutators
const mutators: Mutators = ((form.mutators || {}) as any) as Mutators
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - This casting is a bit gross, but unless we allow parameterized types in the form and expose it all the way through, this is the way it needs to be done. Parameterized types in jsx is already painful so this is not any worse by any means.


mutators.insert('customers', 0, { firstName: '', lastName: '' })
mutators.move('customers', 0, 1)
const customer = mutators.pop('customers')
mutators.push('customers', { firstName: '', lastName: '' })
const removed = mutators.remove('customers', 0)
const shifted = mutators.shift('customers')
mutators.swap('customers', 0, 1)
mutators.unshift('customers', { firstName: '', lastName: '' })
13 changes: 9 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Mutator } from 'final-form'

type DefaultType = { [key: string]: Mutator }

export default DefaultType
export const insert: Mutator
export const move: Mutator
export const pop: Mutator
export const push: Mutator
export const remove: Mutator
export const shift: Mutator
export const swap: Mutator
export const unshift: Mutator

/** The shape of the mutators once final-form has bound them to state */
export type Mutators = {
export interface Mutators {
insert: (name: string, index: number, value: any) => void
move: (name: string, from: number, to: number) => void
pop: (name: string) => any
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"noEmit": true,
"strict": true
},
"include": ["./src/**/*"]
}