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 1 year has passed since last update.

【TypeScript】プリミティブ型

Last updated at Posted at 2022-06-11

目次

number

let year: number = 2000
// year = false
// error TS2322: Type 'boolean' is not assignable to type 'number'.
console.log(year)

let age: number = 0x2b

string

let name: string = "田中"
// name = 10
// error TS2322: Type 'number' is not assignable to type 'string'.
console.log(name)

boolean

let isFinished: boolean = true
isFinished = false
// isFinished = 1 
// error TS2322: Type 'number' is not assignable to type 'boolean'.
console.log({ isFinished })

void

// いかなる返り値も持たないことを明示
function returnNothing(): void {
  console.log("don't return anything")
}

console.log(returnNothing())

null

// nullのみ
let absence: null = null
// absence = "moji"
// error TS2322: Type '"moji"' is not assignable to type 'null'.

undefined

// undefinedのみ
let data: undefined = undefined
// data = 329
// error TS2322: Type '329' is not assignable to type 'undefined'.

any

// いかなる値もエラーはなし
let year: any = 2000
// year = false
// エラーなし
console.log(year)
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?