Skip to content

Commit 0c09378

Browse files
authored
feat(utils): resolve common function ts error (#2903)
1 parent 52e2091 commit 0c09378

File tree

8 files changed

+37
-25
lines changed

8 files changed

+37
-25
lines changed

packages/utils/src/tree-model/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export class Node {
278278
return walkTree(this)
279279
}
280280

281-
insertChild(child, index, batch) {
281+
insertChild(child, index?, batch?) {
282282
if (!child) {
283283
throw new Error('[TINY-Tree] insertChild error: child is required.')
284284
}

packages/utils/src/upload-ajax/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const getError = (action, option, xhr) => {
3838
errorText = `fail to post ${action} ${xhr.status}`
3939
}
4040

41-
const error = new Error(errorText)
41+
const error: any = new Error(errorText)
4242

4343
error.status = xhr.status
4444
error.method = 'post'
@@ -56,7 +56,7 @@ export const uploadAjax = (option) => {
5656
const action = xss.filterUrl(option.action)
5757

5858
if (xhr.upload) {
59-
xhr.upload.onprogress = (event) => {
59+
xhr.upload.onprogress = (event: any) => {
6060
if (event.total > 0) {
6161
event.percent = (event.loaded / event.total) * 100
6262
}

packages/utils/src/validate/rules/required.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
import { hasOwn } from '../../type'
1414
import * as util from '../util'
1515

16-
export default function ({ rule, checkValue, source, errors, options, type }) {
16+
interface RequiredType {
17+
rule: any
18+
checkValue?: any
19+
source: any
20+
errors: any
21+
options: any
22+
type?: any
23+
}
24+
export default function ({ rule, checkValue, source, errors, options, type }: RequiredType) {
1725
if (rule.required && (!hasOwn.call(source, rule.field) || util.isEmptyValue(checkValue, type || rule.type))) {
1826
errors.push(util.format(options.messages.required))
1927
}

packages/utils/src/validate/rules/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ const types = {
8585

8686
export default function (rule, value, source, errors, options) {
8787
if (rule.required && value === undefined) {
88-
required(rule, value, source, errors, options)
88+
let checkValue = value
89+
required({ rule, checkValue, source, errors, options })
8990
return
9091
}
9192

packages/utils/src/validate/schema.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import { format, complementError, asyncMap, warning, deepMerge, convertFieldsError } from './util'
1414
import { hasOwn, isFunction } from '../type'
1515

16-
function Schema(descriptor, translate) {
16+
function Schema(descriptor, translate?) {
1717
Schema.getSystemMessage = () => Schema.getDefaultMessage(translate)
1818
Schema.messages = Schema.getSystemMessage(translate)
1919
Schema.systemMessages = Schema.messages
@@ -30,8 +30,8 @@ function Schema(descriptor, translate) {
3030
*/
3131
const getCompleteFn = (validCallback) => (results) => {
3232
let idx
33-
let errors = []
34-
let fields = {}
33+
let errors = [] as any
34+
let fields = {} as any
3535

3636
function addValid(eror) {
3737
if (Array.isArray(eror)) {
@@ -147,7 +147,7 @@ const setDataRuleOptions = ({ data, options }) => {
147147
const getValidateCallback =
148148
({ failds, doIt }) =>
149149
(errs) => {
150-
const finalErrors = []
150+
const finalErrors = [] as any[]
151151

152152
if (failds && failds.length) {
153153
finalErrors.push(...failds)
@@ -165,14 +165,14 @@ const getValidateCallback =
165165
*/
166166
const asyncCallback =
167167
(options, rule, errorFields, doIt, data) =>
168-
(e = []) => {
169-
let failds = e
168+
(e: any[] | string = []) => {
169+
let failds = e as any
170170
const deep = isDeep(rule, data)
171171

172172
failds = arrayed(failds)
173173

174174
if (!options.suppressWarning && failds.length) {
175-
Schema.warning('async-validator:', failds)
175+
Schema.warning()
176176
}
177177

178178
if (failds.length && rule.message) {
@@ -299,7 +299,7 @@ Schema.prototype = {
299299
validate(source_, o = {}, oc = () => undefined) {
300300
let source = source_
301301
let options = o
302-
let validCallback = oc
302+
let validCallback: Function = oc
303303
if (typeof options === 'function') {
304304
validCallback = options
305305
options = {}
@@ -358,7 +358,7 @@ Schema.prototype = {
358358
}
359359

360360
if (ruleKeys.length === 1 && ruleKeys[0] === 'required') {
361-
return Schema.validators.required
361+
return Schema.validators?.required
362362
}
363363

364364
return Schema.validators[this.getType(rule)] || false
@@ -385,14 +385,16 @@ Schema.register = (type, validator) => {
385385
Schema.validators[type] = validator
386386
}
387387

388-
Schema.validators = {}
388+
Schema.validators = {} as any
389389

390390
Schema.warning = warning
391391

392-
Schema.messages = {}
392+
Schema.messages = {} as any
393393

394-
Schema.systemMessages = {}
394+
Schema.systemMessages = {} as any
395395

396396
Schema.getDefaultMessage = () => undefined
397397

398+
Schema.getSystemMessage = () => undefined
399+
398400
export default Schema

packages/utils/src/validate/util.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function format(i18nTemplate: Function | string, ...rest: string[]) {
6767
return '[Circular]'
6868
}
6969
case '%d':
70-
return Number(rest[i++])
70+
return Number(rest[i++]).toString()
7171
case '%s':
7272
return String(rest[i++])
7373
default:
@@ -109,7 +109,7 @@ function isNativeStringType(type) {
109109
/**
110110
* @description 判断对应的类型是否是空值
111111
*/
112-
export function isEmptyValue(data, dataType) {
112+
export function isEmptyValue(data, dataType?) {
113113
if (isNull(data)) {
114114
return true
115115
}
@@ -135,7 +135,7 @@ export function isEmptyObject(data) {
135135
*/
136136
function asyncParallelArray(arrData, func, callback) {
137137
let count = 0
138-
const results = []
138+
const results = [] as any[]
139139
const arrLength = arrData.length
140140

141141
function checkCount(errors) {
@@ -183,7 +183,7 @@ function asyncSerialArray(arr, fn, cb) {
183183
* @description 将一层数据平铺开
184184
*/
185185
function flattenObjArr(objArr) {
186-
const result = []
186+
const result = [] as any[]
187187

188188
Object.keys(objArr).forEach((item) => {
189189
result.push(...objArr[item])
@@ -197,7 +197,7 @@ function flattenObjArr(objArr) {
197197
*/
198198
export function asyncMap(objArray, option, func, callback) {
199199
if (option.first) {
200-
const pending = new Promise((resolve, reject) => {
200+
const pending = new Promise<void>((resolve, reject) => {
201201
const errorFn = reject
202202
const next = (errors) => {
203203
callback(errors)
@@ -221,8 +221,8 @@ export function asyncMap(objArray, option, func, callback) {
221221
let total = 0
222222
const objArrayKeys = Object.keys(objArray)
223223
const objArrLength = objArrayKeys.length
224-
const results = []
225-
const pending = new Promise((resolve, reject) => {
224+
const results = [] as any[]
225+
const pending = new Promise<void>((resolve, reject) => {
226226
const errorFn = reject
227227
const next = (errors) => {
228228
results.push(...errors)

packages/utils/src/validate/validations/pattern.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { isEmptyValue } from '../util'
1515
import { hasOwn } from '../../type'
1616

1717
export default function (rule, checkValue, callback, source, options) {
18-
const errors = []
18+
const errors = [] as any[]
1919
const validate = rule.required || (!rule.required && hasOwn.call(source, rule.field))
2020

2121
if (validate) {

packages/utils/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"resolveJsonModule": true,
1313
"isolatedModules": true,
1414
"noEmit": true,
15+
"noImplicitAny": false,
1516
"jsx": "preserve",
1617

1718
/* Linting */

0 commit comments

Comments
 (0)