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?

More than 3 years have passed since last update.

【TypeScript】数値を文字列へ変換する方法

Last updated at Posted at 2021-10-26

結論

  • 下記の関数を使用する
    • String()
    • .toString
.ts
const foo: number | undefined  = 0;

const bar = String(foo);
const baz = foo.toString();

console.log(bar); // '0'
console.log(baz); // '0'

String と toString の違い

String

  • undefined を変換した時、 undefined という文字列に変換されてしまう
.ts
const foo: number | undefined  = undefined;

const bar = String(foo);

console.log(bar); // 'undefined'

toString

  • undefined を変換しようとした時、型エラーとなる
.ts
const foo: number | undefined  = undefined;

const bar = foo.toString(); // 型エラー

※文字列を数値へ変換する方法は こちらの記事 に記載

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?