Skip to content
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

Stateless function components for jsx #4861

Closed
Lenne231 opened this issue Sep 18, 2015 · 6 comments
Closed

Stateless function components for jsx #4861

Lenne231 opened this issue Sep 18, 2015 · 6 comments
Assignees
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript

Comments

@Lenne231
Copy link

ReactJS gets stateless function components https://facebook.github.io/react/blog/2015/09/10/react-v0.14-rc1.html

How does it work with the current JSX support in TypeScript? It would be nice if something like this would work as well:

image

@mhegazy
Copy link
Contributor

mhegazy commented Sep 18, 2015

//cc: @RyanCavanaugh

@Lenne231
Copy link
Author

const Headline = (title: string) => <h1>{title}</h1>; doesn't make sense because JSX attributes have names and parameters of a function not.

But const Headline = (props : { title: string }) => <h1>{props.title}</h1>; should work!

@danquirk danquirk added the Bug A bug in TypeScript label Sep 24, 2015
@mhegazy mhegazy added this to the TypeScript 1.7 milestone Sep 28, 2015
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Bug A bug in TypeScript labels Sep 28, 2015
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 1.7 milestone Sep 28, 2015
@RyanCavanaugh RyanCavanaugh removed their assignment Sep 28, 2015
@RyanCavanaugh
Copy link
Member

This needs a spec if we're going to do automatic inference from it.

As @Lenne231 points out, the example posted doesn't seem to be how it works.

Given the example from the release notes:

var Aquarium = ({species}) => (
  <Tank>
    {getFish(species)}
  </Tank>
);

We could type this fairly easily:

// Put this somewhere
interface ReactStateless<T> extends React.Component<T, {}> {
  (props: T): JSX.Element;
}

var Aquarium: ReactStateless<{species: string}> = ({species}) => (
  <Tank>
    {getFish(species)}
  </Tank>
);

@Lenne231
Copy link
Author

Lenne231 commented Oct 9, 2015

Is there a reason why we are currently not inferring the valid JSX components and the matching JSX properties from the React.createElement function? Don't we have all information we need?

type JSX = typeof React.createElement

// Type of valid JSX components: type of the first parameter of JSX
// Type of matching JSX properties: first generic parameter of JSX

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Oct 12, 2015
@Lenne231
Copy link
Author

There will probably be a third kind of component in the future, so it would be nice if the implementation is flexible enough to handle that:

https://github.com/reactjs/react-future/blob/master/07%20-%20Returning%20State/04%20-%20Callback%20Chaining.js

@RyanCavanaugh
Copy link
Member

Fleshing out the design at #5478

@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
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants