无法使打字稿识别打字
Unable to make typescript recognize typing
我正在做这个项目,我们需要将 React Javascript 创建的所有文件翻译成 React Typescript,但作为类型注释的初学者,我遇到了一些困难,尤其是第 3 方包和库。
我正在使用 ReactDatePicker 来获取用户输入的日期(写入或选择)和 moment lib 来解析它,以及多步骤形式的 formik,因此用户可以在一个组件中查看、编辑和创建所有内容。在我篡改的最新文件中,typescript 抱怨我正在使用的 'string' 值无法分配给 'Date',尽管我将其输入为 Date |在我编写代码时在其界面上显示字符串。
这是接口,在外部文件中:
export interface BaseRelationType extends BaseEntity {
institutional_relation_approval: Date | string | any,
}
这是访问它的组件:
import {
getIn, useFormikContext,
} from 'formik';
import DatePicker from 'react-datepicker';
import { FormikStep } from 'components/Stepper/Step';
import DateInput, { DateInputProps } from 'components/DateInput/DateInput';
import { RelationFormType } from 'types/relations';
export interface DateStepProps {
validationSchema: Object,
innerRef: boolean,
}
const RelationTypeStep: React.FC<DateStepProps> = ({
innerRef, validationSchema,
}) => {
const {
values, errors, touched, setFieldValue,
} = useFormikContext<RelationFormType>();
return (
<FormikStep>
<Row>
<DatePicker
selected={values.institutional_relation_approval ? Date.parse(values.institutional_relation_approval) : ''}
placeholderText="DD/MM/YYYY"
dateFormat="dd/MM/yyyy"
name="institutional_relation_approval"
disabled={isView}
onChange={((date) => {
setFieldValue('institutional_relation_approval', date);
console.log(date);
})}
/>
</FormikStep>
);
};
export default RelationTypeStep;
以及我得到的错误:
No overload matches this call.
Overload 1 of 2, '(props: ReactDatePickerProps | Readonly<ReactDatePickerProps>): ReactDatePicker', gave the following error.
Type 'string | number' is not assignable to type 'Date | null | undefined'.
Type 'string' is not assignable to type 'Date | null | undefined'.
Overload 2 of 2, '(props: ReactDatePickerProps, context: any): ReactDatePicker', gave the following error.
Type 'string | number' is not assignable to type 'Date | null | undefined'. TS2769
我是否正确注释了这种类型?这个没有过载错误是什么意思?
不确定是哪一行提示错误。
据我所知,选定的 属性 应该只接受 Date
或 null
Date.parse()
returns number 而不是 Date
else case return 是一个空字符串而不是 null 所以类型不兼容。
selected={
values.institutional_relation_approval
? new Date(values.institutional_relation_approval) // return Date object
: null // return null if no value provided
}
我正在做这个项目,我们需要将 React Javascript 创建的所有文件翻译成 React Typescript,但作为类型注释的初学者,我遇到了一些困难,尤其是第 3 方包和库。
我正在使用 ReactDatePicker 来获取用户输入的日期(写入或选择)和 moment lib 来解析它,以及多步骤形式的 formik,因此用户可以在一个组件中查看、编辑和创建所有内容。在我篡改的最新文件中,typescript 抱怨我正在使用的 'string' 值无法分配给 'Date',尽管我将其输入为 Date |在我编写代码时在其界面上显示字符串。
这是接口,在外部文件中:
export interface BaseRelationType extends BaseEntity {
institutional_relation_approval: Date | string | any,
}
这是访问它的组件:
import {
getIn, useFormikContext,
} from 'formik';
import DatePicker from 'react-datepicker';
import { FormikStep } from 'components/Stepper/Step';
import DateInput, { DateInputProps } from 'components/DateInput/DateInput';
import { RelationFormType } from 'types/relations';
export interface DateStepProps {
validationSchema: Object,
innerRef: boolean,
}
const RelationTypeStep: React.FC<DateStepProps> = ({
innerRef, validationSchema,
}) => {
const {
values, errors, touched, setFieldValue,
} = useFormikContext<RelationFormType>();
return (
<FormikStep>
<Row>
<DatePicker
selected={values.institutional_relation_approval ? Date.parse(values.institutional_relation_approval) : ''}
placeholderText="DD/MM/YYYY"
dateFormat="dd/MM/yyyy"
name="institutional_relation_approval"
disabled={isView}
onChange={((date) => {
setFieldValue('institutional_relation_approval', date);
console.log(date);
})}
/>
</FormikStep>
);
};
export default RelationTypeStep;
以及我得到的错误:
No overload matches this call.
Overload 1 of 2, '(props: ReactDatePickerProps | Readonly<ReactDatePickerProps>): ReactDatePicker', gave the following error.
Type 'string | number' is not assignable to type 'Date | null | undefined'.
Type 'string' is not assignable to type 'Date | null | undefined'.
Overload 2 of 2, '(props: ReactDatePickerProps, context: any): ReactDatePicker', gave the following error.
Type 'string | number' is not assignable to type 'Date | null | undefined'. TS2769
我是否正确注释了这种类型?这个没有过载错误是什么意思?
不确定是哪一行提示错误。
据我所知,选定的 属性 应该只接受 Date
或 null
Date.parse()
returns number 而不是 Date
else case return 是一个空字符串而不是 null 所以类型不兼容。
selected={
values.institutional_relation_approval
? new Date(values.institutional_relation_approval) // return Date object
: null // return null if no value provided
}