LoginSignup
0
0

More than 3 years have passed since last update.

typescript 型について

Last updated at Posted at 2020-09-26

typescriptの型まとめ

// primitive
const apple: number = 5
const speed: string = 'fast'
const hasName: boolean = true

const nothingMuch: null = null
const nothing: undefined = undefined
const now: Date = new Date()

// array
const colors: string[] = ['red', 'green', 'blue']
const myNumbers: number[] = [1, 2, 3]
const truths: boolean[] = [true, false, true]

// class
class Car {}

const car: Car = new Car()

// object literal
const point: { x: number, y: number } = {
  x: 10,
  y: 20
}

const destinations = {
  Japan: {
    longitude: 153,
    latitude: 24
  },
  America: {
    longitude: 38,
    latitude: 97
  }
}
const { Japan }: { Japan: { longitude: number; latitude: number } } = destinations


//any
const every: any[] = [10, 'Jon', true, null, undefined]

// tuple
type Drink = [string, boolean, number]

const pepsi: Drink = ['brown', true, 40]
const sprite: Drink = ['clear', true, 40]
const tea: Drink = ['brown', false, 0]

// interface
interface Person {
  name: string,
  age:number,
  gender: boolean
}

const candidate = (person: Person) => {
  console.log(person.name)
  console.log(person.age)
  console.log(person.gender)
}
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