简体   繁体   English

Typescript React - 类型“字符串”不可分配给类型“外观”

[英]Typescript React - Type 'string' is not assignable to type 'Appearance'

I'm trying to create some UI utility components with React Typescript.我正在尝试使用 React Typescript 创建一些 UI 实用程序组件。 These utilities are meant to provide some default styling to HTML elements.这些实用程序旨在为 HTML 元素提供一些默认样式。

FullScreen is just a div whose height is 100vh and whose width is 100vw . FullScreen只是一个高度为100vh且宽度为100vw的 div。

So FullSreenProps extends HTMLDivElement .所以FullSreenProps扩展HTMLDivElement But when I spread props.style into the style property, I get a type error:但是当我将props.style传播到 style 属性中时,我得到一个类型错误:

Type '{ width: string; height: string; accentColor: string; alignContent: string; alignItems: string; alignSelf: string; alignmentBaseline: string; all: string; animation: string; animationDelay: string; ... 443 more ...; [Symbol.iterator](): IterableIterator<...>; }' is not assignable to type 'CSSProperties'.
  Types of property 'appearance' are incompatible.
    Type 'string' is not assignable to type 'Appearance'.ts(2322)

Here's the FullScreen component:这是FullScreen组件:

import React, { useState, useEffect, useMemo } from 'react';


interface FullScreenProps extends HTMLDivElement{}

const FullScreen = (props: FullScreenProps): JSX.Element => {
    return (
      <div
      {...props}
      style={
          {
            ...props.style,
            width: "100vw", 
            height: "100vh",
          }
      }
      >
      </div>
    );
}
export default FullScreen

What's the correct way to pass props.style while also specificying height: 100vh and width: 100vw in the style prop?传递props.style的正确方法是什么,同时在style属性中指定height: 100vhwidth: 100vw

You can try the following:您可以尝试以下方法:

import React from 'react';


interface FullScreenProps extends Omit<React.HTMLProps<HTMLDivElement>, 'as' | 'ref'> {}

const FullScreen = (props: FullScreenProps): JSX.Element => {
    return (
      <div
      {...props}
      style={
          {
            ...(props.style || {}),
            width: "100vw", 
            height: "100vh",
          }
      }
      >
      </div>
    );
}
export default FullScreen

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' 使用TypeScript进行反应:类型“ {}”不可分配给类型 - React with TypeScript: Type '{}' is not assignable to type 打字稿:类型“字符串”不可分配给类型 - Typescript: Type 'string' is not assignable to type React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' 在反应 typescript 中,类型“字符串”不可分配给类型“从不”并且类型“未知”不可分配给类型“从不”错误 - Type 'string' is not assignable to type 'never' and Type 'unknown' is not assignable to type 'never' error in react typescript TextArea 中的 React Typescript 错误:类型“字符串”不可分配给类型编号 - React Typescript error in TextArea : Type 'string' is not assignable to type number 反应选择和打字稿:类型“字符串”不可分配给类型“ValueType”<OptionTypeBase> &#39; - react-select and typescript: Type 'string' is not assignable to type 'ValueType<OptionTypeBase>' React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState React Typescript不可分配给type参数 - React typescript is not assignable to parameter of type 类型不可分配(React + Typescript + Firebase) - Type not assignable (React + Typescript + Firebase)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM