-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
fix(runtime-core): return boolean type when use defineProps
to set optional boolean prop
#7116
Changes from 7 commits
f5af09f
859abae
054f71d
151cc56
df5656f
2631132
8c750a9
bffb1b2
9a9cbdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,20 @@ describe('defineProps w/ type declaration', () => { | |
// type declaration | ||
const props = defineProps<{ | ||
foo: string | ||
bool?: boolean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this case boolAndUndefined: boolean | undefined |
||
}>() | ||
// explicitly declared type should be refined | ||
expectType<string>(props.foo) | ||
// the Boolean absent props will be cast to false | ||
expectType<boolean>(props.bool) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation will lead it cannot jump to the declaration by clicking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a new PR with another implementation. |
||
// @ts-expect-error | ||
props.bar | ||
}) | ||
|
||
describe('defineProps w/ type declaration + withDefaults', () => { | ||
const res = withDefaults( | ||
defineProps<{ | ||
bool?: boolean | ||
number?: number | ||
arr?: string[] | ||
obj?: { x: number } | ||
|
@@ -33,6 +37,7 @@ describe('defineProps w/ type declaration + withDefaults', () => { | |
z?: string | ||
}>(), | ||
{ | ||
bool: undefined, | ||
number: 123, | ||
arr: () => [], | ||
obj: () => ({ x: 123 }), | ||
|
@@ -53,6 +58,7 @@ describe('defineProps w/ type declaration + withDefaults', () => { | |
// @ts-expect-error | ||
res.y.slice() | ||
|
||
expectType<boolean | undefined>(res.bool) | ||
expectType<string | undefined>(res.x) | ||
expectType<string | undefined>(res.y) | ||
expectType<string>(res.z) | ||
|
@@ -70,7 +76,7 @@ describe('defineProps w/ union type declaration + withDefaults', () => { | |
union1: 123, | ||
union2: () => [123], | ||
union3: () => ({ x: 123 }), | ||
union4: () => 123, | ||
union4: () => 123 | ||
} | ||
) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without
NotUndefined
, the '-?' will still remove 'undefined'.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it won't when there's only
boolean | undefined
and no?:
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgCrIN4ChnIEYD2BANgIIgAmAqpRDKBBQFz5HERwjIA+yArrXohGWAL5YsYAJ4AHFKggBnMMgC8mHMgDaAaWShkAawhSCMNAF0AtAH4WqXRbFYgA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, modified.