Type-hinting functional components?
See original GitHub issueI’m trying to write a simple function-wrapper around the <button>
element.
According to the test-suite:
const ComponentWithChildren: FunctionalComponent<DummerComponentProps> = ({ input, initialInput, children }) => {
return <div>
<span>{initialInput}</span>
<span>{input}</span>
<span>{children}</span>
</div>
}
Am I missing something, or why does this approach not work for me at all?
import { h, FunctionalComponent } from "preact";
export interface ButtonProps {
title: string;
}
export const Button: FunctionalComponent<ButtonProps> = ({ title, children, ...props }) => {
return <button {...props}>{title}</button>;
};
I get a bunch of errors:
Type '(ButtonProps & ComponentProps<FunctionalComponent<ButtonProps>>) | undefined' has no property 'title' and no string index signature.
Type '(ButtonProps & ComponentProps<FunctionalComponent<ButtonProps>>) | undefined' has no property 'children' and no string index signature.
Type '{ key?: any; ref?: ((el: FunctionalComponent<ButtonProps>) => void) | undefined; }' is not assignable to type 'HTMLAttributes'.
Types of property 'ref' are incompatible.
Type '((el: FunctionalComponent<ButtonProps>) => void) | undefined' is not assignable to type '((el?: Element | undefined) => void) | undefined'.
Type '(el: FunctionalComponent<ButtonProps>) => void' is not assignable to type '((el?: Element | undefined) => void) | undefined'.
Type '(el: FunctionalComponent<ButtonProps>) => void' is not assignable to type '(el?: Element | undefined) => void'.
Types of parameters 'el' and 'el' are incompatible.
Type 'Element | undefined' is not assignable to type 'FunctionalComponent<ButtonProps>'.
Type 'undefined' is not assignable to type 'FunctionalComponent<ButtonProps>'.
I tried copy/pasting the entire contents of the test, and it gives similar errors.
I looked at the last successful build from 3 days ago, and the tests didn’t look any different.
The most recent change to the types before that was this, I wonder if that broke something?
I’m using Typescript 2.7.2.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Type-hinting functional components with JSX attributes? #1
What's missing for me, is how to type-hint a functional component that accepts standard JSX attributes? I tried the following: import { h, ......
Read more >typing — Support for type hints — Python 3.11.1 documentation
The Python runtime does not enforce function and variable type annotations. ... to support subscription to denote expected types for container elements.
Read more >Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >Python Type Hints
Python uses dynamic typing, in which variables, parameters, and return values of a function can be any type.
Read more >Custom Python function components | TFX - TensorFlow
In this component definition style, you write a function that is annotated with type hints. The type hints describe the input artifacts, ...
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
Had some time to kill and made a PR (#1033)
Doesn’t seem to help.
Still gives me errors:
Also,
ComponentProps
has been flagged as@deprecated
in themaster
version.I’m afraid the current
tsconfig.json
isn’t configuring the compiler to strictly check the type-declarations - I just tried building the typescript project with--strict
and it’s generating a lot of errors:I write all of my own projects in strict mode, and would strongly recommend the same here - the typescript tests aren’t only interesting to prove that the code can works, but also that the typings are strictly correct, which it looks like they’re not.
In my own projects, if a
.d.ts
fails to compile without warnings, the test has failed.