8000 defineProps 默认对 boolean 类型的 prop 自动转换为 false 不符合语义 · Issue #10674 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content
defineProps 默认对 boolean 类型的 prop 自动转换为 false 不符合语义 #10674
@guaijie

Description

@guaijie

Vue version

3.4.21

Link to minimal reproduction

https://cn.vuejs.org/guide/components/props

Steps to reproduce

一:我很难理解官方回答的:‘’为了对齐原生 HTML 标签的 boolean attributes 的行为‘’
首先 props 和 attributes 在 vue 中本意是不同的,不管是声明,传值还是使用,props 是在 vue 这种框架中独有的,这是你们发明的概念。它不应该贴合所谓的:‘’原生 HTML 标签的 boolean attributes 的行为‘’。它更应该符合直觉(props 和 attributes 本来你就特地做了区分)。
二:就算要满足‘’为了对齐原生 HTML 标签的 boolean attributes 的行为‘’。如果我声明一个类型只是 boolean 类型,你将其结果转换为 false 我能理解。但如果我声明一个类型是'可选的'是可以为 undefined,依然默认转换为 false 那这就很莫名其妙了。这意味着这个所谓的对应原生的 attr 可以传 boolean 也可以什么都不传。那我不传值肯定就是默认 undefined 。官方文档明确说明只有在类型为 boolean 时,默认值才自动转为 false

image

export type SubmitterProps = {
  submitText?: VueNode
  resetText?: VueNode
  buttonGap?: number
  submitButtonProps?: false | (ButtonProps & { preventDefault?: boolean })
  resetButtonProps?: false | (ButtonProps & { preventDefault?: boolean })
  render?: (node: VueNode) => VueNode
  onSubmit?: () => void
  onReset?: () => void
}

上面这个类型声明导致我,不得不做大量的默认值声明。我明确声明了 submitText 是可选的,但依然给我转为了false, 而且 VueNode 里也包含了 undefined 类型。这非常反直觉。而且也不满足你答复的 ‘’为了对齐原生 HTML 标签的 boolean attributes 的行为‘’

What is expected?

如果要满足 ‘’为了对齐原生 HTML 标签的 boolean attributes 的行为‘’ 的话,我建议只有在类型为 boolean 时才默认转换为 false, 当明确指明该 prop 是可选和 undefined 时,默认保持原意:undefined (因为在 js 中没传值就是 undefined)

What is actually happening?

现在的问题在于类型中只要包含了 boolean 类型就默认转换为 false

System Info

No response

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0