首页 > 其他分享 >[Typescript] Type and Interface for performance

[Typescript] Type and Interface for performance

时间:2023-10-10 15:13:54浏览次数:225  
标签:Typescript label interface export Interface performance extends InputProps Compo

Let's say you're creating a component that has all the props of input but needs to add a label prop. You'll need to extend from the ComponentProps type helper

import { ComponentProps } from "react";
 
export type InputProps =
  ComponentProps<"input"> & {
    label: string;
  };
 
export function Input({
  label,
  ...props
}: InputProps) {
  return (
    <label>
      {label}
      <input {...props} />
    </label>
  );
}

But unfortunately, intersections used this way will, on the scale of a large codebase, slow TypeScript down.

Instead, you should use an interface, using interface extends:

import { ComponentProps } from "react";
 
export interface InputProps
  extends ComponentProps<"input"> {
  label: string;
}

 

See article for more details

标签:Typescript,label,interface,export,Interface,performance,extends,InputProps,Compo
From: https://www.cnblogs.com/Answer1215/p/17754712.html

相关文章

  • typescript: Flyweight Pattern
     /***FlyweightPattern享元是一种结构型设计模式,它允许你在消耗少量内存的情况下支持大量对象。*https://refactoringguru.cn/design-patterns/flyweight/typescript/example#lang-features*TheFlyweightstoresacommonportionofthestate(alsocalledintr......
  • TypeScript与JavaScript比较(区别)
     TypeScript和JavaScript是目前项目开发中较为流行的两种脚本语言,TypeScript是JavaScript的一个超集,但是TypeScript与JavaScript之间又有什么样的区别呢?在选择开发语言时,又该如何抉择呢?本文将会深入对比这两种语言,讨论两种语言之间的关联和差异,并概述两种语言各自的优势......
  • 抽象类(abstract)和接口(interface)的区别
    抽象类(abstract)和接口(interface)的区别抽象类(abstract)只有方法名和参数,没有方法体抽象方法一般存在于抽象类中有抽象方法的一定是抽象类抽象类里不一定有抽象方法抽象类被别的类继承(继承只能单继承),子类一定要重写抽象类中的抽象方法,如果子类也是抽象类则不用重写抽......
  • TypeScript基础
    基础类型:":"后面为变量的数据类型布尔值:booleanletisDone:boolean=false数字:numberTypeScript中的所有数字类型都是浮点数,类型统一都是number,支持十进制,二进制,八进制,十六进制。letcount:number=100字符串:stringTypescript中使用string表示文本数据类型,可以使用双引......
  • typescript: Facade Pattern
     /***Facadepattern外观是一种结构型设计模式,能为复杂系统、程序库或框架提供一个简单(但有限)的接口。*TheFacadeclassprovidesasimpleinterfacetothecomplexlogicofoneor*severalsubsystems.TheFacadedelegatestheclientrequeststothe*......
  • TypeScript入门到精通——TypeScript类型系统基础——元组类型
    TypeScript类型系统基础——元组类型 元组(Tuple)表示由有限元素构成的有序列表。在JavaScript中,没有提供原生的元组数据类型。TypeScript对此进行了补充,提供了元组数据类型。由于元组数组之间存在很多共性,因此TypeScript使用数组来表示元组。 在TypeScript中,元组类型......
  • difference between a Client-Server and Sender-Receiver interface in Autosar
    thedifferencebetweenaClient-ServerandSender-ReceiverinterfaceinAutosarInaClient-Serverinterface,theclientrequestsaservicefromtheserverandtheserverrespondswitharesult.InaSender-Receiverinterface,thesendersendsdatatoone......
  • TypeScript入门到精通——TypeScript类型系统基础——数组类型
    数组类型 数组是十分常用的数据结构,它表示一组有序元素的集合。在TypeScript中,数组值的数据类型为数组类型。一、数组类型定义 TypeScript提供了以下两种方式来定义数组类型:简单数组类型表示法泛型数组类型表示法1.1、简单数组类型表示法在TypeScript中,你可以使......
  • typescript: Adapter pattern
     /***Adapterpattern适配器是一种结构型设计模式,它能使不兼容的对象能够相互合作。*file:Adapterts.ts***//***TheTargetdefinesthedomain-specificinterfaceusedbytheclientcode.*/classTarget{publicrequest():string{......
  • typescript: Singleton Pattern
     /***file:Singletonts.ts*SingletonPattern单例是一种创建型设计模式,让你能够保证一个类只有一个实例,并提供一个访问该实例的全局节点。*TheSingletonclassdefinesthe`getInstance`methodthatletsclientsaccess*theuniquesingletoninstance.......