Skip to content

Allow JSX element attributes type to be an intersection #4362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbrantly opened this issue Aug 19, 2015 · 7 comments
Closed

Allow JSX element attributes type to be an intersection #4362

jbrantly opened this issue Aug 19, 2015 · 7 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jbrantly
Copy link

Consider the following:

declare module JSX {
    interface ElementAttributesProperty { props: {}; }
}

class Component<P> {
    props: P & { ref?: string };
}

This results in the error: JSX element attributes type '[type parameter] & { ref?: string; }' must be an object type.

An alternative would be an additional member of the JSX namespace that allowed specifying "global" attributes (that would apply to both value-based and intrinsic elements). For example:

declare module JSX {
    interface GlobalAttributes {
        ref?: string
    }
}

See also DefinitelyTyped/DefinitelyTyped#5347

@RyanCavanaugh
Copy link
Member

Removing this restriction for the sake of intersection types makes sense. It was put in place because we don't really correctly support the JSX Attributes type being a union type.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Aug 19, 2015
@mhegazy mhegazy added this to the Community milestone Aug 19, 2015
@JsonFreeman
Copy link
Contributor

What should happen if it is an intersection of unions? Or an intersection of type parameters that could be instantiated to unions?

@Pajn
Copy link

Pajn commented Nov 20, 2015

A GlobalAttributes interface would be great because it would make it possible to add global attributes without redefining the entire typing file of for example react.

@JsonFreeman
Copy link
Contributor

What does this proposal allow exactly? The attributes type can be any type in the world? Or just an intersection of object types? Keep in mind that an intersection of object types would not fix the Component<P> example above because P is a type parameter.

If the attributes can be any type in the world, what are the semantics for union types, primitives, tuples, and type parameters?

@phiresky
Copy link

My use case is the following:

abstract class TestA<T> extends React.Component<{x:number}&T, {}> {
   ...
}
class TestB extends TestA<{y:number}> {
   render() {...}
}

Declaration works fine, but it can't be used because (I think) of the above problem.

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed Help Wanted You can do this labels Dec 18, 2015
@RyanCavanaugh RyanCavanaugh modified the milestones: TypeScript 1.8, Community Dec 18, 2015
@RyanCavanaugh
Copy link
Member

Fixed in nightly build

@phiresky
Copy link

@RyanCavanaugh Thank you!

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants