Function-type props broken in TypeScript
See original GitHub issueVersion
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
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:24
- Comments:18 (3 by maintainers)
Top Results From Across the Web
Creating a function type that returns argument object with ...
TypeScript allows extraneous properties by default, so you shouldn't need any additional handling to allow those on the return type.
Read more >Handle Props in React in TypeScript
In this article, I will describe some issues you may encounter making illegal props unrepresentable in React. #Handle union of functions; #Use component ......
Read more >TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >Passing Functions in React with TypeScript - Pluralsight
In this component, we create an interface that defines what kind of props our Toggle component should receive. We are expecting only one...
Read more >React prop types with TypeScript - how to have a function type?
Coding example for the question React prop types with TypeScript - how to have a function type?-Reactjs.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 adefault
for a function-type prop. A more complete example:I submitted a fix for this in https://github.com/vuejs/vue/pull/11223.