blog icon indicating copy to clipboard operation
blog copied to clipboard

Typescript 实践中常见的问题

Open huruji opened this issue 5 years ago • 2 comments

导入svg

image

在全局中添加声明:

declare module '*.svg' {
  const content: string
  export default content
}

huruji avatar Jan 07 '20 08:01 huruji

react-router withRouter 的使用

export interface HandleNodeSideContentProps extends RouteComponentProps {
  queue?: string[];
  strategyStore?: IStrategyStore;
}


export default withRouter<HandleNodeSideContentProps>(HandleNodeSideContent);

huruji avatar Jan 09 '20 17:01 huruji

交叉类型中复写类型应该使用 omit 排除相同的 key 交叉类型,每一项的 key 也会进行交叉操作,不排除可能造成类型错误

[❌] type BasicProps = {
  onChange?: (value: string, e: React.MouseEvent<HTMLInputElement>) => void;
}
type SelfProps = {
onChange?: (value: number | string) => void;
}

type IProps = BasicProps & SelfProps

[✅] type BasicProps = {
  onChange?: (value: string, e: React.MouseEvent<HTMLInputElement>) => void;
}
type SelfProps = {
onChange?: (value: number | string) => void;
}

type IProps = Omit<BasicProps, 'onChange'> & SelfProps

huruji avatar May 02 '20 18:05 huruji