question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-hinting functional components?

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
marvinhagemeistercommented, Mar 11, 2018

Had some time to kill and made a PR (#1033)

1reaction
mindplay-dkcommented, Mar 7, 2018

Doesn’t seem to help.

import { h, FunctionalComponent, ComponentProps } from "preact";

export interface ButtonProps extends ComponentProps<any> {
    title?: string;
}

export const Button: FunctionalComponent<ButtonProps> = ({ title, children, ...props }) => {
    return <button {...props}>{title || children}</button>;
};

Still gives me 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.

Also, ComponentProps has been flagged as @deprecated in the master 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:

mindplay@Sidewinder-7240:/mnt/c/workspace/test/preact$ ./node_modules/.bin/tsc -p test/ts/ --strict
test/ts/hoc-test.tsx(18,2): error TS2322: Type 'typeof (Anonymous class)' is not assignable to type 'ComponentConstructor<T & highlightedProps>'.
  Types of parameters 'props' and 'props' are incompatible.
    Type '(T & highlightedProps) | undefined' is not assignable to type 'T & highlightedProps'.
      Type 'undefined' is not assignable to type 'T & highlightedProps'.
        Type 'undefined' is not assignable to type 'T'.
test/ts/hoc-test.tsx(39,70): error TS2345: Argument of type 'typeof SimpleComponent' is not assignable to parameter of type 'ComponentFactory<SimpleComponentProps>'.
  Type 'typeof SimpleComponent' is not assignable to type 'FunctionalComponent<SimpleComponentProps>'.
    Type 'typeof SimpleComponent' provides no match for the signature '(props: RenderableProps<SimpleComponentProps>, context?: any): VNode<any>'.
test/ts/preact.tsx(32,10): error TS2345: Argument of type '({ input, initialInput }: DummerComponentProps) => Element' is not assignable to parameter of type 'string'.
test/ts/preact.tsx(56,10): error TS2564: Property 'refs' has no initializer and is not definitely assigned in the constructor.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found