notes 笔记notes 笔记
Home
Article
Category
Tag
Timeline
Home
Article
Category
Tag
Timeline
  • build

    • 环境变量
  • browse

    • 2fa
    • sse
    • token
  • database

    • mongodb

      • start
    • mysql

      • curd
      • 安装
      • join
      • 多对多
      • 性能优化
      • 表设计
      • 常见问题
  • docker

    • github-actions

      • local
      • prod
    • docker-compose
    • index
    • podman
  • file

    • 文件下载
    • 原生 node.js下载文件
  • git

    • index
    • multiple-github-accounts
    • auto-commit
    • pat
    • rebase
  • linux

    • grep
    • index
    • ssh
    • vim
    • windows
  • network

    • best-proxy-way
    • git-via-https
    • github-push-fail
  • nginx

    • acme.sh
    • cache
    • https
    • index
    • safe
  • node

    • fnm
    • tool
  • obsidian

    • ish
    • start
  • react

    • index
    • set-state
  • summary

    • index
  • vue

    • index
    • typescript
  • libs
  • open-source
  • 两种设置 props 默认值的方法
    • props 解构赋值默认值
    • withDefaults
  • (Array<Foo | Bar>) and (Foo[] | Bar[])
  • ...rest 剩余参数或者 arguments 参数传递的类型错误

两种设置 props 默认值的方法

props 解构赋值默认值

interface IProps {
  type?: 'foo' | 'bar'
}

const { info = 'foo' } = defineProps<IProps>()

withDefaults

interface IProps {
  type?: 'foo' | 'bar'
}

withDefaults(defineProps<IProps>(), {
  type: 'foo'
})

(Array<Foo | Bar>) and (Foo[] | Bar[])

两者在类型上是不同的,这点在使用 map 方法时很明显的报错
https://github.com/microsoft/TypeScript/issues/33591#issuecomment-786443978

...rest 剩余参数或者 arguments 参数传递的类型错误

function foo(a: number, b: number, c: number) {
  return a + b + c
}

// 编译异常
function bar(...rest: number[]) {
  return foo(...rest) // error: Expected 3 arguments, but got 0 or more.
}

// 正确方式
type RestParamType = Parameters<typeof foo> // 使用 Parameters 将 foo 的参数类型挖出来

function bar(...rest: RestParamType) {
  return foo(...rest) // success
}
最近更新:: 2025/10/19 23:44
Contributors: qyhever
Prev
index