1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Expected a `const` instead of a literal type assertion. 解消

Posted at
// No.1
const hoge = 'xl'

// No.2
const hoge = 'xl' as 'xl'

// No.3
const hoge = 'xl' as const

No.1

No.1では、TypeScriptの型推論で、hogeはstring型だと認識する。

No.2

Reactでstring型で渡すのではなく'xl'の型としたい場合は、型アサーションを行う。
リテラルを推測するので、意図した通りになる。(No.2)
しかし、下記のeslintのエラーが表示される。

Expected a `const` instead of a literal type assertion.

No.3

エラー文の通りに修正すると、eslintをパスできる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?