useState

  • Has an initial value and a fixed type
const [validating, setValidating] = React.useState(false)
Copy the code
  • Has an initial value and the type may change
const [validating, setValidating] = React.useState<boolean | string>(false)
Copy the code
  • No initial value
interface IUser { id: string name: string age? : number } const [user, setUser] = React.useState<IUser>()Copy the code

Properties of the current type object are of parent type and not required (optional)

type IChild = Partial<IParent>
Copy the code

The name of an attribute whose type is an object of a certain type

type IKey = keyof IUser
Copy the code

Function arguments are optional if they have default values. Do not add?

(errorMsg: string, k: keyof IUser = 'respMessage') => void
Copy the code

See TypeScript’s advanced usage for more information