Skip to content

Unable to display suggestions with method overloading and generictypes #10151

@mattiamanzati

Description

@mattiamanzati

TypeScript Version: 2.0.0
I am trying to implement a React Hyperscript-like helper in order to avoid using JSX and implement dependency injection. I think there is some trouble with the method overloading I have declared below, because vscode is'nt able to provide correct autosuggestion and typings, and sometimes also tsc itself fails to compile. So I think that either tsc is unable to understand what's going on, or there's something wrong with my code.
Thanks for any further help.

Mattia.

Code

import * as React from 'react'

// just some react type alias
export type ComponentClass<P> = React.StatelessComponent<P> | React.ComponentClass<P>
export type ChildComponent = React.ReactChild

// definition of my "hypescript-like" helper
export function h<P>(Component: string, props?: P & React.DOMAttributes, children?: ChildComponent | ChildComponent[])
export function h<P>(component: ComponentClass<P>, props?: P & React.Attributes, children?: ChildComponent | ChildComponent[])
export function h(component, props?, children?){
    const childrens = children ? children : []
    const componentChildrens = Array.isArray(childrens) ? childrens : [childrens]
    const componentProps = props ? props : {}

    return typeof component === 'string' ?
        React.createElement(component, componentProps, childrens) :
        React.createElement(component, componentProps, childrens)
}

// using this for an HTML tag seems to work
const Button = ({label}: {label: string}) => h('input', {type: 'text'})

// when I start typing label, VSCode and typescript should suggest to me "label" and ensure it's a string
// unfortunately, when I start to write it, I see the DOMAttributes instead and no suggestion for "label".
h(Button, {label: 'test'}

// if I user h<IButtonProps>(Button, {... seems to work nicely

Expected behavior:
After typing h(Button, { vscode should be able to suggest me all the available properties such as "label".

Actual behavior:
After typing h(Button, { DOMAttributes completions are shown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions