0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React Vite TypeScript 【TypeScript編】はじめてのTypeScript

Posted at

React + Vite + TypeScriptで開発したい

自分はどの技術も触ったことがない。
まずはTypeScriptから勉強するぞ

javascriptやってたし多分いける

TypeScriptの!の使い方

まず環境構築を済ませて、生成されたファイルに目を通した

main.tsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>,
)


createRoot()の引数として記述されているtypescript:getElementById('root')
その後ろにが付いている...

こんな記述は見たことがなかった。
コード内にあるは条件式などに使う演算子くらいif(!flag)しか見たことなかったのでどういう意図が全く分からなかった

結論

この非null アサーションというらしい。
値がnullやundefindではないと保証するために使うらしい

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>,
)

つまりrootというidの要素が見つからなくても処理を続ける

というわけではなく
nullundefindeを返す可能性があるけど
開発者が返す可能性はないと保証するために使うらしい。

なにがどう便利なのか想像がつかないが知識として持っておきます

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?