Skip to content

Commit 4922d9c

Browse files
committed
Convert to TypeScript
1 parent fe3c260 commit 4922d9c

38 files changed

+812
-676
lines changed

.babelrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
}
1010
}
1111
],
12-
"@babel/preset-flow"
12+
"@babel/preset-typescript"
1313
],
1414
"plugins": [
15-
"@babel/plugin-transform-flow-strip-types",
1615
"@babel/plugin-syntax-dynamic-import",
1716
"@babel/plugin-syntax-import-meta",
1817
"@babel/plugin-proposal-class-properties",

eslint.config.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import js from '@eslint/js'
2+
import tsPlugin from '@typescript-eslint/eslint-plugin'
3+
import tsParser from '@typescript-eslint/parser'
4+
import globals from 'globals'
5+
6+
export default [
7+
js.configs.recommended,
8+
{
9+
files: ['**/*.{js,jsx,ts,tsx,mjs}'],
10+
languageOptions: {
11+
parser: tsParser,
12+
ecmaVersion: 2020,
13+
sourceType: 'module',
14+
globals: {
15+
...globals.browser,
16+
...globals.node,
17+
...globals.jest
18+
}
19+
},
20+
plugins: {
21+
'@typescript-eslint': tsPlugin
22+
},
23+
rules: {
24+
...tsPlugin.configs.recommended.rules,
25+
'@typescript-eslint/no-explicit-any': 'off',
26+
'@typescript-eslint/no-unused-vars': [
27+
'error',
28+
{ argsIgnorePattern: '^_' }
29+
],
30+
'no-unused-vars': 'off'
31+
}
32+
},
33+
{
34+
ignores: [
35+
'node_modules/**',
36+
'dist/**',
37+
'coverage/**',
38+
'.nyc_output/**',
39+
'*.config.js',
40+
'package-scripts.js',
41+
'src/index.d.test.ts'
42+
]
43+
}
44+
]

package-scripts.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
)
3535
),
3636
es: {
37-
description: 'run the build with rollup (uses rollup.config.js)',
37+
description: 'run the build with rollup (uses rollup.config.mjs)',
3838
script: 'rollup --config --environment FORMAT:es'
3939
},
4040
cjs: {
@@ -57,37 +57,19 @@ module.exports = {
5757
description: 'Generates table of contents in README',
5858
script: 'doctoc README.md'
5959
},
60-
copyTypes: series(
61-
npsUtils.copy('src/*.js.flow src/*.d.ts dist'),
62-
npsUtils.copy(
63-
'dist/index.js.flow dist --rename="final-form-arrays.cjs.js.flow"'
64-
),
65-
npsUtils.copy(
66-
'dist/index.js.flow dist --rename="final-form-arrays.es.js.flow"'
67-
)
68-
),
60+
copyTypes: series('tsc --declaration --emitDeclarationOnly --outDir dist'),
6961
lint: {
7062
description: 'lint the entire project',
7163
script: 'eslint .'
7264
},
73-
flow: {
74-
description: 'flow check the entire project',
75-
script: 'flow check'
76-
},
7765
typescript: {
7866
description: 'typescript check the entire project',
79-
script: 'tsc'
67+
script: 'tsc --noEmit'
8068
},
8169
validate: {
8270
description:
8371
'This runs several scripts to make sure things look good before committing or on clean install',
84-
default: concurrent.nps(
85-
'lint',
86-
'flow',
87-
'typescript',
88-
'build.andTest',
89-
'test'
90-
)
72+
default: concurrent.nps('lint', 'typescript', 'build.andTest', 'test')
9173
}
9274
},
9375
options: {

package.json

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"scripts": {
1313
"start": "nps",
1414
"test": "nps test",
15-
"precommit": "lint-staged && npm start validate"
15+
"precommit": "lint-staged && npm start validate",
16+
"build:types": "tsc --declaration --emitDeclarationOnly --outDir dist",
17+
"prebuild": "yarn build:types"
1618
},
1719
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
1820
"license": "MIT",
@@ -25,52 +27,55 @@
2527
},
2628
"homepage": "https://github.com/final-form/final-form-arrays#readme",
2729
"devDependencies": {
28-
"@babel/core": "^7.5.4",
29-
"@babel/plugin-external-helpers": "^7.0.0",
30-
"@babel/plugin-proposal-class-properties": "^7.5.0",
31-
"@babel/plugin-proposal-decorators": "^7.4.4",
32-
"@babel/plugin-proposal-export-namespace-from": "^7.5.2",
33-
"@babel/plugin-proposal-function-sent": "^7.5.0",
34-
"@babel/plugin-proposal-json-strings": "^7.0.0",
35-
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
36-
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
37-
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
38-
"@babel/plugin-syntax-import-meta": "^7.0.0",
39-
"@babel/plugin-transform-flow-strip-types": "^7.4.4",
40-
"@babel/plugin-transform-runtime": "^7.5.0",
41-
"@babel/preset-env": "^7.5.4",
42-
"@babel/preset-flow": "^7.0.0",
30+
"@types/jest": "^29.5.14",
31+
"@babel/core": "^7.27.1",
32+
"@babel/plugin-external-helpers": "^7.27.1",
33+
"@babel/plugin-proposal-class-properties": "^7.18.6",
34+
"@babel/plugin-proposal-decorators": "^7.27.1",
35+
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
36+
"@babel/plugin-proposal-function-sent": "^7.27.1",
37+
"@babel/plugin-proposal-json-strings": "^7.18.6",
38+
"@babel/plugin-proposal-numeric-separator": "^7.18.6",
39+
"@babel/plugin-proposal-throw-expressions": "^7.27.1",
40+
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
41+
"@babel/plugin-syntax-import-meta": "^7.10.4",
42+
"@babel/plugin-transform-flow-strip-types": "^7.27.1",
43+
"@babel/plugin-transform-runtime": "^7.27.1",
44+
"@babel/preset-env": "^7.27.2",
45+
"@babel/preset-typescript": "^7.27.1",
4346
"babel-core": "^7.0.0-bridge.0",
44-
"babel-eslint": "^10.0.2",
45-
"babel-jest": "^24.8.0",
46-
"bundlesize": "^0.18.0",
47-
"doctoc": "^1.3.0",
48-
"eslint": "^6.0.1",
49-
"eslint-config-react-app": "^3.0.6",
50-
"eslint-plugin-babel": "^5.3.0",
51-
"eslint-plugin-flowtype": "^3.2.1",
52-
"eslint-plugin-import": "^2.16.0",
53-
"eslint-plugin-jsx-a11y": "^6.2.1",
54-
"eslint-plugin-react": "^7.13.0",
55-
"final-form": "^4.20.8",
56-
"flow-bin": "^0.102.0",
47+
"babel-eslint": "^10.1.0",
48+
"babel-jest": "^29.7.0",
49+
"bundlesize": "^0.18.2",
50+
"doctoc": "^2.2.1",
51+
"@eslint/js": "^9.27.0",
52+
"@typescript-eslint/eslint-plugin": "^8.32.1",
53+
"@typescript-eslint/parser": "^8.32.1",
54+
"eslint": "^9.27.0",
55+
"globals": "^16.2.0",
56+
"eslint-plugin-import": "^2.31.0",
57+
"eslint-plugin-jsx-a11y": "^6.10.2",
58+
"eslint-plugin-react": "^7.37.5",
59+
"final-form": "^5.0.0-3",
5760
"glow": "^1.2.2",
58-
"husky": "^3.0.0",
59-
"jest": "^24.8.0",
60-
"lint-staged": "^9.2.0",
61-
"nps": "^5.9.5",
62-
"nps-utils": "^1.5.0",
63-
"prettier": "^1.18.2",
64-
"prettier-eslint-cli": "^5.0.0",
65-
"react": "^16.8.6",
66-
"rollup": "^1.16.7",
67-
"rollup-plugin-babel": "^4.3.3",
68-
"rollup-plugin-commonjs": "^10.0.1",
61+
"husky": "^9.1.7",
62+
"jest": "^29.7.0",
63+
"lint-staged": "^16.0.0",
64+
"nps": "^5.10.0",
65+
"nps-utils": "^1.7.0",
66+
"prettier": "^3.5.3",
67+
"prettier-eslint-cli": "^8.0.1",
68+
"react": "^19.1.0",
69+
"rollup": "^4.41.1",
70+
"rollup-plugin-babel": "^4.4.0",
71+
"rollup-plugin-commonjs": "^10.1.0",
6972
"rollup-plugin-flow": "^1.1.1",
7073
"rollup-plugin-node-resolve": "^5.2.0",
7174
"rollup-plugin-replace": "^2.2.0",
72-
"rollup-plugin-uglify": "^6.0.2",
73-
"typescript": "^3.5.3"
75+
"rollup-plugin-uglify": "^6.0.4",
76+
"rollup-plugin-typescript2": "^0.36.0",
77+
"typescript": "^5.8.3",
78+
"ts-jest": "^29.2.5"
7479
},
7580
"peerDependencies": {
7681
"final-form": "^4.20.8"
@@ -82,10 +87,21 @@
8287
]
8388
},
8489
"jest": {
90+
"preset": "ts-jest",
8591
"testEnvironment": "node",
8692
"testPathIgnorePatterns": [
87-
".*\\.ts"
88-
]
93+
"/node_modules/",
94+
"src/index.d.test.ts"
95+
],
96+
"moduleFileExtensions": [
97+
"ts",
98+
"tsx",
99+
"js",
100+
"jsx"
101+
],
102+
"transform": {
103+
"^.+\\.(ts|tsx)$": "ts-jest"
104+
}
89105
},
90106
"bundlesize": [
91107
{

rollup.config.js renamed to rollup.config.mjs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import resolve from 'rollup-plugin-node-resolve'
22
import babel from 'rollup-plugin-babel'
3-
import flow from 'rollup-plugin-flow'
3+
import typescript from 'rollup-plugin-typescript2'
44
import commonjs from 'rollup-plugin-commonjs'
55
import { uglify } from 'rollup-plugin-uglify'
66
import replace from 'rollup-plugin-replace'
7+
import ts from 'typescript'
78

89
const minify = process.env.MINIFY
910
const format = process.env.FORMAT
@@ -33,7 +34,7 @@ if (es) {
3334
}
3435

3536
export default {
36-
input: 'src/index.js',
37+
input: 'src/index.ts',
3738
output: Object.assign(
3839
{
3940
name: 'final-form-arrays',
@@ -43,8 +44,21 @@ export default {
4344
),
4445
external: [],
4546
plugins: [
46-
resolve({ jsnext: true, main: true }),
47-
flow(),
47+
resolve({
48+
mainFields: ['module', 'jsnext:main', 'main'],
49+
browser: true,
50+
preferBuiltins: false
51+
}),
52+
typescript({
53+
typescript: ts,
54+
clean: true,
55+
tsconfigOverride: {
56+
compilerOptions: {
57+
declaration: false,
58+
declarationMap: false
59+
}
60+
}
61+
}),
4862
commonjs({ include: 'node_modules/**' }),
4963
babel({
5064
exclude: 'node_modules/**',
@@ -57,12 +71,10 @@ export default {
5771
modules: false,
5872
loose: true
5973
}
60-
],
61-
'@babel/preset-flow'
74+
]
6275
],
6376
plugins: [
6477
['@babel/plugin-transform-runtime', { useESModules: !cjs }],
65-
'@babel/plugin-transform-flow-strip-types',
6678
'@babel/plugin-syntax-dynamic-import',
6779
'@babel/plugin-syntax-import-meta',
6880
'@babel/plugin-proposal-class-properties',

src/concat.test.js renamed to src/concat.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
import concat from './concat'
2+
import { MutableState, Tools } from 'final-form'
23

34
describe('concat', () => {
4-
const getOp = value => {
5+
const getOp = (value: any) => {
56
const changeValue = jest.fn()
6-
concat(['foo', value], {}, { changeValue })
7+
const mockState: MutableState<any> = {
8+
fieldSubscribers: {},
9+
fields: {},
10+
formState: {
11+
values: {}
12+
}
13+
} as any
14+
concat(['foo', value], mockState, { changeValue } as unknown as Tools<any>)
715
return changeValue.mock.calls[0][2]
816
}
917

1018
it('should call changeValue once', () => {
1119
const changeValue = jest.fn()
12-
const state = {}
13-
const result = concat(['foo', ['bar', 'baz']], state, { changeValue })
20+
const state: MutableState<any> = {
21+
fieldSubscribers: {},
22+
fields: {},
23+
formState: {
24+
values: {}
25+
}
26+
} as any
27+
const result = concat(['foo', ['bar', 'baz']], state, { changeValue } as unknown as Tools<any>)
1428
expect(result).toBeUndefined()
1529
expect(changeValue).toHaveBeenCalled()
1630
expect(changeValue).toHaveBeenCalledTimes(1)
@@ -32,4 +46,4 @@ describe('concat', () => {
3246
expect(Array.isArray(result)).toBe(true)
3347
expect(result).toEqual(['a', 'b', 'c', 'd', 'e'])
3448
})
35-
})
49+
})
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// @flow
2-
import type { MutableState, Mutator, Tools } from 'final-form'
1+
import { MutableState, Mutator, Tools } from 'final-form'
32

43
const concat: Mutator<any> = (
54
[name, value]: any[],
65
state: MutableState<any>,
76
{ changeValue }: Tools<any>
8-
) => {
9-
changeValue(state, name, (array: ?(any[])): any[] =>
7+
): void => {
8+
changeValue(state, name, (array?: any[]): any[] =>
109
array ? [...array, ...value] : value
1110
)
1211
}
1312

14-
export default concat
13+
export default concat

src/copyField.js renamed to src/copyField.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// @flow
2-
import type { InternalFieldState } from 'final-form/dist/types'
3-
41
function copyField(
5-
oldFields: { [string]: InternalFieldState },
2+
oldFields: { [key: string]: any },
63
oldKey: string,
7-
newFields: { [string]: InternalFieldState },
4+
newFields: { [key: string]: any },
85
newKey: string
9-
) {
6+
): void {
107
newFields[newKey] = {
118
...oldFields[oldKey],
129
name: newKey,
@@ -32,4 +29,4 @@ function copyField(
3229
}
3330
}
3431

35-
export default copyField
32+
export default copyField

src/index.d.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// tslint:disable no-console
22

3-
import { Config, createForm, AnyObject } from 'final-form'
3+
import { Config, createForm } from 'final-form'
44
import arrayMutators from './index'
55
import { Mutators } from './index'
66

7-
const onSubmit: Config['onSubmit'] = (values, callback) => {}
7+
const onSubmit: Config['onSubmit'] = (_values, _callback) => { }
88

99
const form = createForm({
1010
mutators: { ...arrayMutators },

0 commit comments

Comments
 (0)