我的组件代码是:

import styled from "styled-components/native";

export const GlobalButton = styled.TouchableOpacity`
  border-radius: 10px;
  background-color: ${(props) => (props["bc"] ? props["bc"] : "#2962ff;")};
  width: ${(props) => (props["width"] ? props["width"] + "px;" : "250px;")};
  height: ${(props) => (props["height"] ? props["height"] + "px;" : "35px;")};
  margin-top: 10px;
  align-items: center;
  justify-content: center;
`;

用法

<GlobalButton width={340} onPress={handleConfirm}>
  <GradientButton>
    <GlobalText color="#FFFF" fontSize={18} fw={340}>
      Confirmar
    </GlobalText>
  </GradientButton>
</GlobalButton>

文字抱怨我的props colored颜色 ,字体大小和字体,怎么纠正这个?

推荐答案

您应该为您的GlobalButton定义类型:

import styled from "styled-components/native";

export const GlobalButton = styled.TouchableOpacity< color?: string; fontSize?: number; fw?: number >`
  border-radius: 10px;
  background-color: ${(props) => (props["bc"] ? props["bc"] : "#2962ff;")};
  width: ${(props) => (props["width"] ? props["width"] + "px;" : "250px;")};
  height: ${(props) => (props["height"] ? props["height"] + "px;" : "35px;")};
  margin-top: 10px;
  align-items: center;
  justify-content: center;
`;

Typescript相关问答推荐

如何在Jest中嘲笑location.back()方法调用进行Angular 组件测试?

TypScript在对象上强制执行值类型时推断对象文字类型?

参数类型undefined不能分配给参数类型字符串|未定义

类型安全JSON解析

有没有一种方法可以在保持类型的可选状态的同时更改类型?

如何判断输入是否是TypeScript中的品牌类型?

无法绑定ngModel.条件ngSwitchCase中的错误

在函数中打字推断记录的关键字

使用2个泛型类型参数透明地处理函数中的联合类型

正确使用相交类型的打字集

为什么我在这个重载函数中需要`as any`?

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

创建依赖函数参数的类型

将超类型断言为类型脚本中的泛型参数

Angular NG8001错误.组件只能在一个页面上工作,而不是在她的页面上工作

如何处理可能为空的变量...我知道这不是Null?

可以将JS文件放在tsconfig';s includes/files属性?或者,我如何让tsc判断TS项目中的JS文件?

组件使用forwardRef类型脚本时传递props

是否使用类型将方法动态添加到类?

如何根据Typescript 中带有泛型的对象嵌套键数组获取数组或对象的正确类型?