首页 > 其他分享 >[React Typescript] Overriding and Removing Component Props

[React Typescript] Overriding and Removing Component Props

时间:2023-05-22 14:12:20浏览次数:32  
标签:Typescript const Removing Omit Component value props onChange type

Using Omit

import { ComponentProps } from 'react';
import { Equal, Expect } from '../helpers/type-utils';

export const Input = (
  props: Omit<ComponentProps<'input'>, 'onChange'> & {
    onChange: (value: string) => void;
  }
) => {
  return (
    <input
      {...props}
      onChange={(e) => {
        props.onChange(e.target.value);
      }}
    ></input>
  );
};

const Parent = () => {
  return (
    <Input
      onChange={(e) => {
        console.log(e);

        type test = Expect<Equal<typeof e, string>>;
      }}
    ></Input>
  );
};

 

Better

import { ComponentProps } from 'react';
import { Equal, Expect } from '../helpers/type-utils';

type InputProps = Omit<ComponentProps<'input'>, 'onChange'> & {
    onChange: (value: string) => void;
  }

export const Input = (
  props: InputProps
) => {
  return (
    <input
      {...props}
      onChange={(e) => {
        props.onChange(e.target.value);
      }}
    ></input>
  );
};

const Parent = () => {
  return (
    <Input
      onChange={(e) => {
        console.log(e);

        type test = Expect<Equal<typeof e, string>>;
      }}
    ></Input>
  );
};

 

Even Better:

type OverrideProps<T, TOverriden> = Omit<T, keyof TOverridden> & TOverridden;

type InputProps = OverrideProps<ComponentProps<"input">, {onChange: (value: string) => void}>

 

Using interface:

interface InputProps extends Omit<ComponentProps<"input">, "onChange"> {
  onChange: (value: string) => void;
}

 

标签:Typescript,const,Removing,Omit,Component,value,props,onChange,type
From: https://www.cnblogs.com/Answer1215/p/17420445.html

相关文章

  • [React Typescript] Useful React Prop Type Examples
    RelevantforcomponentsthatacceptotherReactcomponentsasprops.exportdeclareinterfaceAppProps{children?:React.ReactNode;//best,acceptseverythingReactcanrenderchildrenElement:JSX.Element;//AsingleReactelementstyle?:React.C......
  • AngularJS2 教程 -- TypeScript环境配置
    AngularJS2TypeScript环境配置这开始前,你需要确保你已经安装了npm,如果你还没安装npm或者不了解npm可以查看我们的教程:NPM使用介绍。由于npm官网镜像国内访问太慢,这里我使用了淘宝的npm镜像,安装方法如下:$npminstall-gcnpm--registry=https://registry.npm.taobao.org......
  • vue component:is 组件切换
    <template><Child1/><Child2/><component:is="currentComp"></component><el-button@click="compChange">切换组件</el-button></template><scriptsetup>import{shallo......
  • 08_TypeScript 循环
    08_TypeScript循环有的时候,我们可能需要多次执行同一块代码。一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推。编程语言提供了更为复杂执行路径的多种控制结构。for循环TypeScriptfor循环用于多次执行一个语句序列,简化管理循环变量的代码......
  • webpack搭建typescript项目
    基于webpack的typescript项目搭建该项目是搭建了一个数值转换的前端项目,使用了webpack,typescript重点学习webpack.config.js的配置/Users/song/Code/webgpu_learn/webgpu-for-beginners/03typescript/finished/index.html<!DOCTYPEhtml><htmllang="en"><head><m......
  • 07_TypeScript 条件语句
    07_TypeScript条件语句条件语句通常在写代码时,您总是需要为不同的决定来执行不同的动作。您可以在代码中使用条件语句来完成该任务。在TypeScript中,我们可使用以下条件语句:if语句-只有当指定条件为true时,使用该语句来执行代码if...else语句-当条件为true时执行......
  • TypeScript中的定时器
    在ts中使用setInterval()和setTimeout()时,如果把特们的返回值定义成number类型,那么ts会警告你不能将类型“Timeout”分配给类型“number”或你不能将类型“Timer”分配给类型“number”原因setInterval()和setTimeout()使用的是Node.js下的接口定义,而在ts中,window下......
  • 企业级项目模板的配置与集成(Vite + Vue3 + TypeScript)
    企业级项目模板的配置与集成(Vite+Vue3+TypeScript)1、项目介绍项目使用:eslint+stylelint+prettier来对我们代码质量做检测和修复。需要使用husky来做commit拦截需要使用commitlint来统一提交规范需要使用preinstall来统一包管理工具。2、环境准备nodev16.14.2pnp......
  • 从0到1搭建后台管理系统(Vue3 + Vite4 + TypeScript + Element Plus + Pinia + Vue Rou
    参考有来:https://www.cnblogs.com/haoxianrui/p/17331952.htmlNode16+版本大于16【问题一:目前我是14.18.2的版本npm是6.14.15版本,这就涉及到要切换node版本的问题,不然我安装了16我的vue2项目就启动不了了】vscode插件市场搜索 VueLanguageFeatures(Volar) 和 TypeScript......
  • TypeScript 类型
    类型判断getType:function(obj:any){returnObject.prototype.toString.call(obj).slice(8,-1);}//=>[objectFunction/xxxx]=>‘Function/xxxx’Object.prototype.toString.call(obj))是一种常用的typeof,使用了object原型上的toString。该值存放于对象的[Sym......