LoginSignup
2
2

More than 3 years have passed since last update.

【TypeScript】Type 'string' is not assignable to type 'number | undefined'.の対処

Posted at

はじめに

Reactでzeit-ui/reactのstyleを適用させようとすると、以下のコンパイルエラーとなった。

エラー内容

Type 'string' is not assignable to type 'number | undefined'.ts(2322)

numberのところにstringのtypeを割り当てることはできません。

そうですね。
できませんよね。

対処

import * as React from "react";
import { Image } from '@zeit-ui/react'

export function SampleImage() {
  let width: number
  return <Image
    width="540"
    height="160"
    src="http://www.deelay.me/2000/https://react.zeit-ui.co/images/custom-domains.png" />;
}

この様にしていたのを、


width={540}
height={160}

このように修正するとエラーが消えました。

終わりに。

最後まで読んで頂きありがとうございます:bow_tone1:
転職の為、未経験の状態からRailsを学習しております。正しい知識を着実に身に着け、実力のあるエンジニアになりたいと考えています。継続して投稿していく中で、その為のインプットも必然的に増え、成長に繋がるかと考えています。
投稿の内容に間違っているところや、付け加えるべきところが多々あるかと思いますので、ご指摘頂けると幸いです。この記事を読んで下さりありがとうございました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2