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

Factory-resolved JSX namespaces #18131

Closed
DanielRosenwasser opened this issue Aug 29, 2017 · 3 comments
Closed

Factory-resolved JSX namespaces #18131

DanielRosenwasser opened this issue Aug 29, 2017 · 3 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

Current issues

Two recent issues we've observed relatively frequently with our JSX implementation includes the following:

  1. Two separate versions of a @types/react can have differing declarations of JSX. This is problematic because the JSX namespace is global and these conflicts are surfaced to users rather than library authors. This makes for a bad experience. (Code sharing between multiple React projects causing TS2403 #17111)
  2. Users can't mix and match different JSX implementations within a project. (how can i mix 2 JSX framework in the same project? #15386)

Mitigations

For each of the above issues respectively:

  1. Currently, you can use path mapping to ensure that TypeScript has a single reference for whenever libraries import "react" or some such library
  2. There is no mitigation I can think of.

Idea

@mhegazy and I have discussed this, and believe it is worth exploring the idea of resolving JSX namespaces from the same module as the factory function itself. In other words, if the JSX factory is React.createElement, TypeScript should attempt to resolve the JSX namespace from the location of the factory function itself.

Code could temporarily be defined that works in both older and newer versions of TypeScript:

namespace JSX {
    // JSX stuff goes in here.
}
export { /*...*/ }

import Temp_JSX = JSX;
declare global {
    import JSX = Temp_JSX;
}

Duplicate JSX declaration errors would still be a problem (and potentially exacerbated), but after several versions we could remove the global augmentations.

Open questions:

  1. How does this work with imports/aliases of the JSX factory itself?
@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 29, 2017
@DanielRosenwasser DanielRosenwasser changed the title Module-scoped JSX Namespace Factory-resolved JSX Namespaces Aug 29, 2017
@DanielRosenwasser DanielRosenwasser changed the title Factory-resolved JSX Namespaces Factory-resolved JSX namespaces Aug 29, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2017

//cc @RyanCavanaugh

@kitsonk
Copy link
Contributor

kitsonk commented Aug 30, 2017

Two separate versions of a @types/react can have differing declarations of JSX. This is problematic because the JSX namespace is global and these conflicts are surfaced to users rather than library authors. This makes for a bad experience.

This isn't just a problem for React. It is a problem for any multi package project where you have cross dependencies and you are trying to work with TypeScript. Projects that have different versions of @types/node or in situations where there is a common library between the two. It would be great to see a more general solution to that.

@aronallen
Copy link

I really like this idea.

@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 28, 2018
@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed In Discussion Not yet reached consensus labels Feb 28, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants