- boolean
- string
- number
- undefined
- null
- any
- unkown
- never
- void
- bigint
- symbol
// this is enforce
const returnSomething = (a: number, b:number):number => {
return a + b
}
const returnSomething = (a: number, b:number) => {
return a + b
}
// typescript know that this function is a number
- type is more flexible than interface
type fooType = {
foo: string
}
type todoType = fooType &{
userId: number
id: number
title: string
completed: boolean
}
const fetchSomething = async(id: number): Promise<todoType> => {
const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`)
const data = await res.json() as todoType // casting, this is overide not enforce
return data
}
(async() => {
fetchSomething(1).then((data) => {
console.log(data)
})
})()
interface fooType {
foo: string
}
interface todoType extends fooType{
userId: number
id: number
title: string
completed: boolean
}
type APIResponseType<T> = {
message: string
results: T
}
type UserType = {
id: number
name: string
}
const fetchUsers = ():APIResponseType<UserType> => {
const results = {id: 1, name: 'foo' }
return {message: 'Success', results} as APIResponseType<UserType>
}
type UserType = {
id: string
name: string
age?: number
email: string
}
// ? optional
// this will become like user but without id and email
type CreateUser = Omit<UserType, 'id' | 'email'>
// all of the UpdateUser become optional
type UpdateUser = Partial<Omit<UserType, 'id'>>
// only name
type SomethingUser = Pick<UserType, 'name' | 'email'>
// this will remove ? optional, all of them become required
type SomethingUser2 = Required<UserType>