8000 Supporting optional boolean properties in TypeScript · Issue #396 · vue-macros/vue-macros · GitHub
[go: up one dir, main page]

Skip to content

Supporting optional boolean properties in TypeScript #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
matthew-dean opened this issue Jun 14, 2023 · 1 comment
Open
3 tasks done

Supporting optional boolean properties in TypeScript #396

matthew-dean opened this issue Jun 14, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@matthew-dean
Copy link

Clear and concise description of the problem

I've documented the full issue here. Essentially, TypeScript is not mapped consistently in the case of optional booleans because of legacy Vue behavior. A type of:

interface Props {
  foo?: boolean
}

... which in TypeScript means the type of foo is boolean | undefined (with a default value of undefined) is defined as boolean (with a default value of false) when mapped to the Vue JS API, which is contrary to TypeScript's behavior.

Suggested solution

As suggested in the above, if Vue doesn't accept this feature request (or doesn't address it for some time), to do the following:

  1. If a TypeScript type is passed into withDefaults(defaultProps, {}), any boolean type should be required in the withDefaults object, so that the developer can make it clear if they are intending the default Vue behavior, or the default TypeScript behavior.
  2. Ideally, a withDefaults macro would be required with a defaultProps macro if, again, there are boolean types in the interface, but that could be considered too high of a burden for developers.

Alternative

The alternative is I guess to educate all developers that booleans behave weird in Vue and differently than other types? And then to use a withDefaults macro to always set the default value of each boolean as undefined.

Additional context

No response

Validations

@sxzz
Copy link
Member
sxzz commented Aug 7, 2024

Vue exhibits a boolean cast behavior.

The code <Comp checked /> compiles to <Comp checked="" />, aligning with the HTML specification. However, this forces Vue to interpret an empty string as true, resulting in unusual boolean casting.

We may address this issue with a feature flag in the future.

Right now, my primary focus is on Vue Vapor, so I don't have time for these tasks in the short term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
0