Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTypeScript: Typed Function Props #9692
Comments
posva
added
the
typescript
label
Mar 13, 2019
added a commit
to pikax/vue
that referenced
this issue
Mar 18, 2019
added a commit
to pikax/vue
that referenced
this issue
Mar 18, 2019
yyx990803
closed this
in
df4af4b
Mar 19, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maxmellen commentedMar 13, 2019
•
edited
What problem does this feature solve?
Get type checking for function passed as prop to a component.
Currently, it is possible to use
PropType
to get better typing on props passed to a component.For example:
In this example, the
user
prop will be correctly typed by the TypeScript type checker.This however doesn't work with function props, the following example...
... yields the following error:
What does the proposed API look like?
As converting to
unknown
first is somewhat unwieldy (and seems not to work in a SFC Vetur setup, maybe this should be looked into separately), it would be nice to have a helper function exposed by Vue such as the following...... which would allow writing the following:
I would be willing to write a PR for this, but I'm not sure where to put the definition of the
TypedFunctionProp
function as it can't be put in the declaration file.I'm also completely open to having a different name for that function. Maybe
FunctionPropType
is more consistent. I've actually switched toFnPropType
in the project I'm working on, as it's relatively short, and is consistent with thePropType
name.