-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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(types): fix function prop type inference #11223
base: dev
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Vue, { VNode } from "../index"; | ||
import { ComponentOptions } from "../options"; | ||
import { ComponentOptions, PropType } from "../options"; | ||
|
||
class Test extends Vue { | ||
a: number = 0; | ||
|
@@ -154,6 +154,45 @@ const FunctionalScopedSlotsComponent = Vue.extend({ | |
} | ||
}); | ||
|
||
declare function assertBoolean<A>(value: string extends A ? never : (A extends boolean ? A : never)): true; | ||
|
||
declare const val1: boolean; | ||
// declare const val2: boolean | (() => boolean); | ||
// declare const val3: any; | ||
|
||
assertBoolean(val1); //=> compiles (good) | ||
// assertBoolean(val2); //=> does not compile (good) | ||
// assertBoolean(val3); //=> does not compile (good) | ||
|
||
const ComponentWithFunctionProps = Vue.extend({ | ||
props: { | ||
functionProp: { | ||
type: Function, | ||
default: () => true, | ||
}, | ||
functionPropWithBooleanReturnType: { | ||
type: Function as PropType<() => boolean>, | ||
default: () => true, | ||
}, | ||
booleanProp: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
booleanPropWithFunctionDefault: { | ||
type: Boolean, | ||
default: () => true, | ||
}, | ||
}, | ||
methods: { | ||
test(): void { | ||
this.functionProp(); // callable (good) | ||
assertBoolean(this.functionPropWithBooleanReturnType()) | ||
Comment on lines
+188
to
+189
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. For reference... Before the change, these lines were failing with: this.functionProp();
assertBoolean(this.functionPropWithBooleanReturnType());
|
||
assertBoolean(this.booleanProp); | ||
assertBoolean(this.booleanPropWithFunctionDefault); | ||
}, | ||
}, | ||
}); | ||
|
||
const Parent = Vue.extend({ | ||
data() { | ||
return { greeting: 'Hello' } | ||
|
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.
To explain a little: calling
assertBoolean(value)
will only compile ifvalue
is preciselyboolean
, and not ifvalue
isboolean | AnotherType
, nor ifvalue
isany
(the latter is why...lue: string extends A ? nev...
is there—it's just a way to throw ifvalue
isany
).