我如何在React中使用d.ts文件

时间:2019-03-25 16:29:05

标签: reactjs typescript types react-final-form

我想在我的React项目中使用d.ts类型文件,但无法确定是否需要import X from "..."或它是否自动填充到我的项目中。

我尝试了典型的import X from "...",但是由于不允许从“ .d.ts”文件导入而无法正常工作。如果我只是尝试使用附加的接口,则由于未导入外部文件,因此无法识别外部文件。

index.d.ts

import * as React from 'react';
import {
  FormApi,
  Config,
  Decorator,
  FormState,
  FormSubscription,
  FieldSubscription
} from 'final-form';

export interface FieldRenderProps<T extends HTMLElement> {
  input: {
    name: string;
    onBlur: (event?: React.FocusEvent<T>) => void;
    onChange: (event: React.ChangeEvent<T>) => void;
    onFocus: (event?: React.FocusEvent<T>) => void;
    value: any;
    checked?: boolean;
  };
...
}
...

TextField.tsx-要使用这些类型的文件。在这种情况下,道具输入等...

...
const TextField = ({ input, meta, label, icon, ...props }) => {
  return (
    <Flex flexDirection="column">
      {label ? <Label htmlFor={props.name}>{label}</Label> : null}
      <StyledTextField pl={icon ? 5 : 2} {...input} {...props} />
      {meta && meta.error && meta.touched && (
        <ErrorMessage>{meta.error}</ErrorMessage>
      )}
    </Flex>
  );
};
...

我需要将这些类型添加到道具input, meta, label, icon, ...props

0 个答案:

没有答案
相关问题