Skip to content

Commit ba6dd39

Browse files
CarlosBonettierikras
authored andcommitted
Add typescript definition file and test (#12)
* Add typescript typings * Add typescript checking script at validate phase
1 parent e4c65d3 commit ba6dd39

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

package-scripts.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,20 @@ module.exports = {
7272
description: 'flow check the entire project',
7373
script: 'flow check'
7474
},
75+
typescript: {
76+
description: 'typescript check the entire project',
77+
script: 'tsc'
78+
},
7579
validate: {
7680
description:
7781
'This runs several scripts to make sure things look good before committing or on clean install',
78-
default: concurrent.nps('lint', 'flow', 'build.andTest', 'test')
82+
default: concurrent.nps(
83+
'lint',
84+
'flow',
85+
'typescript',
86+
'build.andTest',
87+
'test'
88+
)
7989
}
8090
},
8191
options: {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"jsnext:main": "dist/final-form-calculate.es.js",
88
"module": "dist/final-form-calculate.es.js",
99
"files": ["dist"],
10+
"typings": "dist/index.d.ts",
1011
"scripts": {
1112
"start": "nps",
1213
"test": "nps test",
@@ -56,7 +57,8 @@
5657
"rollup-plugin-flow": "^1.1.1",
5758
"rollup-plugin-node-resolve": "^3.3.0",
5859
"rollup-plugin-replace": "^2.0.0",
59-
"rollup-plugin-uglify": "^3.0.0"
60+
"rollup-plugin-uglify": "^3.0.0",
61+
"typescript": "^2.7.2"
6062
},
6163
"peerDependencies": {
6264
"final-form": ">=1.3.0"

src/index.d.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import createDecorator from './'
2+
3+
createDecorator(
4+
{
5+
field: 'foo',
6+
updates: {
7+
bar: (value, allValues) => {
8+
return value
9+
},
10+
baz: (value, allValues) => {
11+
return value
12+
},
13+
},
14+
},
15+
{
16+
field: ['bar', 'baz'],
17+
updates: (value, field, allValues) => {
18+
return { ...allValues }
19+
},
20+
},
21+
{
22+
field: /ba/,
23+
isEqual: (a, b) => a === b,
24+
updates: (value, field, allValues) => {
25+
return { ...allValues }
26+
},
27+
}
28+
)

src/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Decorator } from 'final-form'
2+
3+
export type FieldName = string
4+
5+
export type FieldPattern = FieldName | RegExp | FieldName[]
6+
7+
export type UpdatesByName = {
8+
[FieldName: string]: (value: any, allValues?: Object) => any
9+
}
10+
11+
export type UpdatesForAll = (
12+
value: any,
13+
field: string,
14+
allValues?: Object,
15+
) => { [FieldName: string]: any }
16+
17+
export type Updates = UpdatesByName | UpdatesForAll
18+
19+
export type Calculation = {
20+
field: FieldPattern,
21+
updates: Updates,
22+
isEqual?: (a: any, b: any) => boolean,
23+
}
24+
25+
export default function createDecorator(
26+
...calculations: Calculation[]
27+
): Decorator

tsconfig.json

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

0 commit comments

Comments
 (0)