GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Parse and check type arguments on JSX opening and self-closing tags by weswigham · Pull Request #22415 · microsoft/TypeScript

Conversation

@weswigham

@weswigham Wesley Wigham (weswigham) commented Mar 8, 2018

Copy link
Copy Markdown
Member

Allows, eg,

<MyComponent<number> data={12} />

in .tsx files.

Fixes #6395.

Was there a list of things we were concerned wouldn't parse well? AFAIK, this is actually easy for us to unambiguously parse.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sanex3339, newbalancem5, sergey-tyan, yuit, niba, BANG88, piotrwitek, bradenhs, bykbtzr, danielkcz, and 129 more reacted with thumbs up emoji sanex3339, yuit, AzkiyOne, Bnaya, demensky, raveclassic, danthegoodman, bfncs, markozxuu, Nebu1eto, and 10 more reacted with laugh emoji pelotom, sanex3339, qcz, Lenne231, danielkcz, AzkiyOne, ragnarstolsmark, homburg, nashspence, dead-claudia, and 63 more reacted with hooray emoji sanex3339, forabi, sue71, ccorcos, massimosiani, nashspence, twobit, Bnaya, aspirisen, demensky, and 38 more reacted with heart emoji
All reactions
  • 👍 139 reactions
  • 😄 20 reactions
  • 🎉 73 reactions
  • ❤️ 48 reactions
@weswigham
Parse and check type arguments on JSX opening like elements
23f1e8d
@weswigham
Wesley Wigham (weswigham) requested review from Ryan Cavanaugh (RyanCavanaugh), Mohamed Hegazy (mhegazy) and Ron Buckton (rbuckton) March 8, 2018 22:50
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Mar 9, 2018
Design Meeting Notes, 3/9/2018 #22445
Closed
@ajafff Klaus Meinhardt (ajafff) mentioned this pull request Mar 9, 2018
no-inferred-empty-object: check JsxOpeningLikeElement fimbullinter/wotan#119
Closed
Sheetal Nandi (sheetalkamat) added a commit to microsoft/TypeScript-TmLanguage that referenced this pull request Mar 13, 2018
@sheetalkamat
Support the JSX Opening or self closing tag's type arguments
aef7be5
This handles support for microsoft/TypeScript#22415
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) reviewed Mar 15, 2018
View reviewed changes
Comment thread src/compiler/checker.ts Outdated
min = Math.min(min, getMinTypeArgumentCount(sig.typeParameters));
max = Math.max(max, length(sig.typeParameters));
}
const paramCount = min < max ? min + "-" + max : min;

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Mar 15, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would find min === max ? min : min + "-" + max clearer

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) reviewed Mar 15, 2018
View reviewed changes
Comment thread src/compiler/checker.ts Outdated
}

function getTypeArgumentArityError(node: Node, signatures: Signature[], typeArguments: NodeArray<TypeNode>) {
let min = Number.POSITIVE_INFINITY;

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Mar 15, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the original author didn't just use Infinity / -Infinity ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) approved these changes Mar 15, 2018
View reviewed changes

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have nitty comments in moved code that can be ignored 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@weswigham
Fix nits
9e3f5b5
@weswigham
Wesley Wigham (weswigham) merged commit a909000 into microsoft:master Mar 22, 2018
@weswigham
Wesley Wigham (weswigham) deleted the jsx-type-arguments branch March 22, 2018 22:07
@mohsen1 Mohsen Azimi (mohsen1) mentioned this pull request Mar 29, 2018
What is the right way to use generic components with JSX? #3960
Closed
@aaronjensen

Aaron Jensen (aaronjensen) commented Apr 3, 2018

Copy link
Copy Markdown

This is great! Is there a related issue on Babel? What is the process for ensuring they stay up to date?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Apr 3, 2018

Copy link
Copy Markdown
Member Author

I don't think we have a formal process right now, but pinging Andy (Andrewkraft) (@Andy-MS) seems like a decent way to check. 😝

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjorasch cjorasch mentioned this pull request Apr 4, 2018
Build breaks with certain property data types including generics and JSX.Element stenciljs/core#350
Closed
1 task done
@jonaskello Jonas Kello (jonaskello) mentioned this pull request Apr 5, 2018
Typescript JSX generic component elements prettier/prettier#4259
Closed
@ikatyang Ika (ikatyang) mentioned this pull request Apr 5, 2018
New: support generic JSX element (fixes #462) eslint/typescript-eslint-parser#461
Merged
@mvestergaard

Mathias Vestergaard (mvestergaard) commented Apr 11, 2018

Copy link
Copy Markdown

Wesley Wigham (@weswigham) Really great this finally made it into the language!

Question: When tsconfig is set to "tsx": "preserve", is the generic removed?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Apr 11, 2018

Copy link
Copy Markdown
Member Author

It aught to be. If not, it's a bug.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Apr 13, 2018

Copy link
Copy Markdown
Member

Is there any sense of in what release this will land?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Apr 14, 2018

Copy link
Copy Markdown
Member Author

2.9 which will be released inside two months, given our bimonthly release cadence 😄

andrewbranch, HenrikFricke, danthegoodman, wezleytsai, lednhatkhanh, and capaj reacted with thumbs up emoji HenrikFricke, danthegoodman, geowarin, dimitropoulos, and lednhatkhanh reacted with hooray emoji HenrikFricke, danthegoodman, and lednhatkhanh reacted with heart emoji
All reactions
  • 👍 6 reactions
  • 🎉 5 reactions
  • ❤️ 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Apr 17, 2018
TypeScript 2.9 meta-issue babel/babel#7747
Closed
TypeScript type arguments on JSX tags babel/babel#7748
Closed
TypeScript 2.9 meta-issue microsoft/TypeScript-TmLanguage#603
Closed
This was referenced Apr 24, 2018
Support for type arguments in JSX constructors microsoft/TypeScript-TmLanguage#606
Closed
TypeScript: Support type arguments on JSX opening and self-closing tags babel/babel#7799
Merged
@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Apr 27, 2018
Typescript hover hints show wrong types #23747
Closed
@marty-wang

Mo (marty-wang) commented May 4, 2018

Copy link
Copy Markdown

It seems that one has to explicitly specify the type, as oppose to that the compiler can infer the type from the value.

What I mean is that in JSX, say I have the component below

interface IProps<T> {
    value: T;
    children: (value: T) => React.ReactNode;
}

export class MyComp<T extends {}> extends React.Component<IProps<T>> {
    public render() {
        return this.props.children(this.props.value);
    }
}

when I use MyComp, in the code below, specify the type number, otherwise the val variable will not have type of number.

<MyComp<number> value={4}>
    {val => {
       // the type of val is number
        return "";
    }}
</MyComp>

However, If I omit specifying the type number, the val variable will be type of {}.

<MyComp value={4}>
    {val => {
       // the type of val is {}
        return "";
    }}
</MyComp>

This seems to be a bug, because compare to Generics in regular class, like below., the type of number does not have to be explicitly specified.

class A<T> {
    constructor(public val: T) {
    }
}

const a = new A(5)
a.val // val is type of number
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented May 4, 2018

Copy link
Copy Markdown
Member

Mo (@marty-wang) IIUC, I think what you're seeing is #22636

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marty-wang

Mo (marty-wang) commented May 4, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Andrew Branch (@andrewbranch) yes, it is the same issue. In #22636 Mohamed Hegazy (@mhegazy) seems saying it would be addressed in 2.9. However, at least it is not yet in 2.9.0-dev.20180503.
Really hope that it will be fixed by the official release of 2.9

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented May 4, 2018

Copy link
Copy Markdown
Member Author

#23492 has yet to be merged

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marty-wang

Mo (marty-wang) commented May 4, 2018

Copy link
Copy Markdown

Wesley Wigham (@weswigham) sweet! thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@majelbstoat Jamie Talbot (majelbstoat) mentioned this pull request May 22, 2018
[Question] How to extend Query component apollographql/react-apollo#1980
Closed
@jorySsense

Justin Bieber (jorySsense) commented Jun 11, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Question,

What would be best way to pass a generic type to a child component?

interface GenericProps<T> {
  isFetching: boolean;
  items: T[];
  removeItem: () => Promise<any>;
  history: History;
}

class ListPage<T> extends React.Component<GenericProps<T>> {
  render() {
    return <LayoutCard {...this.props} />
      <ListWrapper<T> items={this.props.items}>
        {this.props.children}
      </ListWrapper>
    </LayoutCard>
  }
};
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Jun 11, 2018

Copy link
Copy Markdown
Member Author

What you have there aught to be correct.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jorySsense

Justin Bieber (jorySsense) commented Jun 11, 2018

Copy link
Copy Markdown

Wesley Wigham (@weswigham) i was getting a

Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<ListWithModalConsumer<{}>> & Readonly<{ children?:...'.   Type '{}' is not assignable to type 'Readonly<ListPageProps<{}>>'
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jorySsense

Justin Bieber (jorySsense) commented Jun 11, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Oh i guess i should of mentioned that List accepts a generic type

export class ListWrapper<T> extends React.Component<ListWrapperProps<T>> {

  render() {
    const { items, children } = this.props;
    return <ModalConsumer>
      {({ showModal, hideModal }) =>
        <List style={{ width: '100%' }} items={items}
              render={(item: T) => children({ item, hideModal, showModal })}>
        </List>
      }
    </ModalConsumer>
  }
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Jun 11, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

You need to constrained your T to at least have the members required by the component you're invoking. In your example, T has no constraint, so it is overriding the type implied by your addition of the items prop and checking against the {} implied by the unconstrained T. (Or intersect the desired props type with T instead of just passing T)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@smashercosmo Vladislav Shkodin (smashercosmo) mentioned this pull request Jun 20, 2018
Implement jsx generics meta/flow#6495
Closed
@sandangel Sandy (sandangel) mentioned this pull request Jul 9, 2018
Consider JSX-like syntax inside dart code flutter/flutter#11609
Closed
@jameslaneconkling

James Conkling (jameslaneconkling) commented Aug 16, 2018

Copy link
Copy Markdown

is there a non-jsx equivalent for this? e.g.

given this usage w/ jsx

<MyGenericComponent<SomeType> {...props} />

what is the equivalent using createElement?

createElement(StringInput, props)

// [ts] Type '{}' is not assignable to type 'SomeType'.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nickserv

Nicky McCurdy (nickserv) commented Aug 16, 2018

Copy link
Copy Markdown
Contributor

I believe the first generic parameter of createElement is equivalent:

createElement<SomeType>(MyGenericComponent, props)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jameslaneconkling

James Conkling (jameslaneconkling) commented Aug 16, 2018

Copy link
Copy Markdown

The type signature of createElement is

    function createElement<P>(
        type: SFC<P> | ComponentClass<P> | string,
        props?: Attributes & P | null,
        ...children: ReactNode[]): ReactElement<P>;

so looks like the first parameter is Props

nickserv reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ecwyne Eric Wyne (ecwyne) mentioned this pull request Oct 14, 2018
Upgrade to TypeScript version 2.9 codesandbox/codesandbox-client#880
Closed
@onlyann Yann Normand (onlyann) mentioned this pull request Nov 13, 2018
[react-big-calendar] add a new generic for resource type DefinitelyTyped/DefinitelyTyped#30401
Merged
7 tasks done
@jamesisaac James Isaac (jamesisaac) mentioned this pull request Apr 26, 2019
Explicit type specification for component rendering <MyComponent<T> /> meta/flow#7672
Closed
@maxbrunsfeld Max Brunsfeld (maxbrunsfeld) mentioned this pull request Aug 21, 2019
Use TSX parser instead of Typescript parser for JavaScript and JSX github/semantic#229
Merged
zero-t4
Nadir (zero-t4) reviewed Jan 24, 2020
View reviewed changes

@zero-t4 Nadir (zero-t4) left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@a-tarasyuk Oleksandr Tarasiuk (a-tarasyuk) mentioned this pull request Mar 31, 2020
Incomplete type cleanup with jsx set to preserve #37456
Closed
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) RyanCavanaugh approved these changes
@rbuckton Ron Buckton (rbuckton) Awaiting requested review from rbuckton
@mhegazy Mohamed Hegazy (mhegazy) Awaiting requested review from mhegazy
+1 more reviewer
@zero-t4 Nadir (zero-t4) zero-t4 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

10 participants

@weswigham @aaronjensen @mvestergaard @andrewbranch @marty-wang @jorySsense @jameslaneconkling @nickserv @RyanCavanaugh @zero-t4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.