首页 > 其他分享 >[Typescript] Write clean Type 3 - make a wrapper to cleanup generic usages

[Typescript] Write clean Type 3 - make a wrapper to cleanup generic usages

时间:2023-04-17 14:57:57浏览次数:40  
标签:usages Typescript const generic color theme CSSProperties useStyled

Original code:

import { CSSProperties } from "react";

const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => {
  // Imagine that this function hooks into a global theme
  // and returns the CSSProperties
  return {} as CSSProperties;
};

interface MyTheme {
  color: {
    primary: string;
  };
  fontSize: {
    small: string;
  };
}

const buttonStyle = useStyled<MyTheme>((theme) => ({
  color: theme.color.primary,
  fontSize: theme.fontSize.small,
}));

const divStyle = useStyled<MyTheme>((theme) => ({
  backgroundColor: theme.color.primary,
}));

Notice that everytime we call useStyled<MyTheme>, we have to pass generic. We want a way that we don't need to pass generic.

 

Solution:

import { CSSProperties } from 'react';

const makeUseStyle = <TTheme = {}>() => {
  return (func: (theme: TTheme) => CSSProperties) => {
    return {} as CSSProperties;
  };
};
const useStyled = makeUseStyle<MyTheme>();

interface MyTheme {
  color: {
    primary: string;
  };
  fontSize: {
    small: string;
  };
}

const buttonStyle = useStyled((theme) => ({
  color: theme.color.primary,
  fontSize: theme.fontSize.small,
}));

const divStyle = useStyled((theme) => ({
  backgroundColor: theme.color.primary,
}));

Now when use call useStyle, we don't need to pass generic slot everytime.

This solution allows you to pass the generic once in the code and all the rest of code will get infer of those generic type.

 

标签:usages,Typescript,const,generic,color,theme,CSSProperties,useStyled
From: https://www.cnblogs.com/Answer1215/p/17325829.html

相关文章

  • TypeScript type 关键字和 interface 关键字
    前言type和interface都可以声明TS类型。typePoint1={x:number;y:number;};interfacePoint2{x:number;y:number;};它们两个声明这个对象类型有任何区别吗?很明显没有,我认为最能区分它们两个的标志是,type有一个=赋值等号。typetype可以做类......
  • TypeScript 报错:Type '({ filename: string; createTime: string; filePath: string;
    问题:因为TypeScript不支持直接给一个接口类型的变量赋一个未知的值。如consta:A={ name:'s'};你需要给这样的对象或数组值使用as指定一个类型。正确写法:consta:A={ name:'s'}asA;数组写法一样:consta:A[]=[ { name:'s' }]asA[];使用as将一......
  • typescript vue3 VueDraggable 报错 Uncaught TypeError: Cannot read properties of
    UncaughtTypeError:Cannotreadpropertiesofnull(reading'element')nnotreadpropertiesofnull(reading'index')错误写法就是说子组件需要用div包着,你用其他东西,他无法添加key,然后就会报错。<template#item="{element}"><Todo:detail=......
  • TypeScript:高级类型
    class类型class类型,和Java差不多。classPerson{name:string;age:number=0;}letperson=newPerson();构造函数classPerson{name:string;age:number=0;constructor(name:string,age:number){this.name=name;this.age=age;......
  • TypeScript:函数兼容性问题
    函数兼容性大家在JS中经常可以看到这样的代码:lisr.forEach((item)=>{});lisr.forEach((item,index)=>{});就是()的参数有时是可以省略的,而这个正式函数的兼容性性质。说白了就是:多的函数=少的函数;参数少的函数可以赋值给参数多的举个下面例子:typeFun1=(num1......
  • TypeScript:字面量类型
    我是做Java的,看到这个字面量类型震惊我三观,原来设计类型可以这么草率。先说吧,字符串、数字、对象、布尔类型的常量值,可以作为字面量类型。是的,是常量值作为类型。举个下面例子:constname:'zhangsan'='zhangsan'constage:18=18constisMan:true=trueconstcar:{user:......
  • TypeScript:类型断言
    TS中有类型推断的机制,就是你不指定常量类型,TS会自动帮你推动,如下:但他这种推断机制,是基于高层的父元素,很多子元素的方法和属性使用用不到,这样我们得手动的进行类型断言,使用[as元素类型],如下:如果你不知道此标签的元素类型,可以通过下面的方式:找到自己知道类型的标签类型,指着......
  • [Typescript] Generic slots, using built-in types as much as possible
    Therearetwooptionsdoingthesamethings.constmakeSafe=<TParamsextendsany[],TReturn>(func:(...args:TParams)=>TReturn)=>(...args:TParams):|{type:"success";result:TReturn;}......
  • OpenTiny 跨端、跨框架组件库升级TypeScript,10万行代码重获新生
    摘要:一份精心准备的《JS项目改造TS指南》文档供大家参考,顺便介绍TS基础知识和TS在Vue中的实践。本文分享自华为云社区《历史性的时刻!OpenTiny跨端、跨框架组件库正式升级TypeScript,10万行代码重获新生!》,作者:Kagol。根据TheSoftwareHouse发布的《2022前端开发市场状......
  • 免费分享前端面试题,vue面试题,TypeScript基础知识点 PDF格式
    免费分享前端资料,面试题,电子书接前端开发,带徒弟,一对一教学,远程协助,bug修改微信:......