vue: Function-type props broken in TypeScript
Version
2.5.22
Reproduction link
https://jsfiddle.net/keegan_openbay/gehkx7pf/10/
https://jsfiddle.net/keegan_openbay/018rs3ae/11/
(More explanation in the fiddle, but keep in mind that JSFiddle doesn’t show TS errors)
Steps to reproduce
- Declare a prop of type
Function, and with a default function that returns some value; e.g.,
// ...
props: {
fooFn: {
type: Function,
default: () => true,
},
},
// ...
- Try to use that function elsewhere in your component options; e.g.,
// ...
methods: {
useFooFn(): void {
const bar = this.fooFn();
// ...
},
},
// ...
What is expected?
type FooFn = typeof this.fooFn; // Function
this.fooFn(); // no errors
What is actually happening?
type FooFn = typeof this.fooFn; // boolean | Function
this.fooFn();
// Cannot invoke an expression whose type lacks a call signature.
// Type 'boolean | Function' has no compatible call signatures.
Vue version: 2.5.22 TypeScript version: 3.0.3
tsconfig.json:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es7", "dom"],
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"baseUrl": "./app/javascript",
"noImplicitThis": true
},
"include": [
"app/javascript/**/*.ts",
"app/javascript/**/*.tsx",
"app/javascript/**/*.vue"
],
"exclude": [
"**/*.spec.ts",
"node_modules"
],
"compileOnSave": false
}
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 25
- Comments: 18 (3 by maintainers)
Commits related to this issue
- fix(types): fix function prop type inference close #9357 — committed to kjleitz/vue by kjleitz 4 years ago
if you annotate with the
PropType<>it should work, this was a fix on https://github.com/vuejs/vue/pull/9733there’s an PR https://github.com/vuejs/vuejs.org/pull/2068 to update docs
This is still broken, even with
"strict": true; can’t use adefaultfor a function-type prop. A more complete example:I submitted a fix for this in https://github.com/vuejs/vue/pull/11223.
Reverting https://github.com/vuejs/vue/pull/8537, specifically this change:
…fixes the
Function-type prop issue.@posva
I tried adding the test case to the project and couldn’t reproduce: