Skip to content

Improving IntelliSense for React Components in JavaScript #15251

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
mjbvz opened this issue Apr 18, 2017 · 7 comments
Closed

Improving IntelliSense for React Components in JavaScript #15251

mjbvz opened this issue Apr 18, 2017 · 7 comments
Labels
Question An issue which isn't directly actionable in code VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Apr 18, 2017

From: microsoft/vscode#24926

Problem
For a simple react component defined in a JavaScript file:

class MyComponent extends Component {
    render() {
       ...
    }
}

I was unable to figure out how to get proper intellisense inside of Component. Specific problems:

  • No suggestions for members of Component provided inside of MyComponent when typing this.
  • No way to specify the type of props or state using jsdocs.

microsoft/vscode#24926 suggested that we use the PropTypes object for IntelliSense. I'm not sure this is feasible, although someone could probably write a TSServer plugin to do this.

However, I also wasn't able to write a jsdoc that provides proper IntelliSense here. Is there any way to do this currently?

@mjbvz mjbvz added the VS Code Tracked There is a VS Code equivalent to this issue label Apr 18, 2017
@arackaf
Copy link

arackaf commented Apr 18, 2017

I may be missing something obvious, but why would using propTypes not be feasible? Why would

static propTypes = {
    x: PropTypes.string,
    y: PropTypes.number,
    submoduleStore: PropTypes.instanceOf(store)
}

inside a class not be doable to infer type bindings for this.props.

@mjbvz
Copy link
Contributor Author

mjbvz commented Apr 18, 2017

Someone on the TypeScript team may be able to speak to this better, but I don't think we can pick up types from a static property like that. A TypeScript server extension may be able to enrich TypeScript's knowledge of the component however

@arackaf
Copy link

arackaf commented Apr 18, 2017

I guess I'm speaking more on the VS Code side, than TypeScript. I'm not sure if VS Code does any sort of type inference on its own, or it's just limited to what TypeScript would do.

I can see why TypeScript wouldn't bake in React support to their language; I had just assumed VS Code would be able to, assuming the file is javascriptreact or typescriptreact, be able to sniff out propTypes on a class component definition.

But no complaints - we're really loving VS Code :)

@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2017

You could use @augments to specify the propTypes as well as the type of the state for a react component. for instance:

/**
 * @augments {Component<{x: string, y:number}}, State>}
 */
class MyComponent extends Component {
...
}

Now the type of this.props should be {x: string, y: number} and the type of of this.sate should be State.

As for bake-in inferences for react, we have always shied away from baking-in any type inferences for frameworks. we believe a better approach is to add more power to the language to allow users to explicitly declare their intent, hence the @augments support.

@arackaf
Copy link

arackaf commented Apr 18, 2017

@mhegazy thank you SO much. Worked like a charm!

btw I definitely don't think TypeScript should bake in React stuff - that comment should have been made on the VS Code issue, not here. Apologies, and thanks again!

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 19, 2017
@mjbvz
Copy link
Contributor Author

mjbvz commented May 31, 2017

Closing since I believe the original question has been answered

@Alphapage
Copy link

Alphapage commented May 23, 2018

I tried this workaround, but it doesn't seem to work with such a syntax:

export default withStyles(styles)( 
class MyComponent extends Component {

}
)

I tried:

/**
 * @augments {Component<{x: string, y:number}}, State>}
 */
export default withStyles(styles)( 
class MyComponent extends Component {

}
)

or


export default withStyles(styles)( 
/**
 * @augments {Component<{x: string, y:number}}, State>}
 */
class MyComponent extends Component {

}
)

without success.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

No branches or pull requests

5 participants