首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于custom.d.ts的类型记录界面属性增强

基于custom.d.ts的类型记录界面属性增强
EN

Stack Overflow用户
提问于 2022-10-01 23:02:39
回答 1查看 98关注 0票数 3

我需要修改TypographyProps { variants:‘主’,‘备用’},并通过类型扩展将新的变体添加到变体中。

示例:

我有单身症

代码语言:javascript
复制
packages/
----- web-1
----- web-2
----- ui-library
---------- Typography/Typography.tsx


export interface DefaultVariant {
  variant: 'variant-1' | 'variant-3' | 'variant-2'
}

export interface VariantOverrides {}

type Merge<X, Y> = {
  [K in keyof X | keyof Y]:
    | (K extends keyof X ? X[K] : never)
    | (K extends keyof Y ? Y[K] : never)
}

export interface TypographyProps {
  variant: Merge<DefaultVariant, VariantOverrides>
}

export const Typography = ({ variant }: TypographyProps) => {
  return <div>{variant}</div>
}




// types in web-1 package
declare module 'ui-library' {
  import 'ui-library'

  export interface VariantOverrides {
    variant: 'variant-4' | 'variant-5'
  }
}

// types in web-2 package
declare module 'ui-library' {
  import 'ui-library'

  export interface VariantOverrides {
    variant: 'variant-55' | 'variant-46'
  }
}

有人知道为什么这不管用吗?非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-02 14:56:11

这种方法是可能的,仅仅是枚举,因为类型不能增加。

代码语言:javascript
复制
import * as React from 'react';
import Button from './packages/common/components/Button';


declare global {
  namespace Common {
    enum Variants {
      v2 = 'v2',
    }
  }
}

export default function App() {
  return <Button variants="v1" />;
}



/// 


import * as React from 'react';

declare global {
  namespace Common {
    enum Variants {
      v1 = 'v1',
    }

    interface ButtonProps {
      variants: keyof typeof Variants;
    }
  }
}

function Button({ variants }: Common.ButtonProps) {
  return <button>Test</button>;
}

export default Button;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73922290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档