Skip to content

Allow the use of a module JSX namespace #8757

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
ceymard opened this issue May 23, 2016 · 11 comments
Closed

Allow the use of a module JSX namespace #8757

ceymard opened this issue May 23, 2016 · 11 comments
Labels
Duplicate An existing issue was already created

Comments

@ceymard
Copy link

ceymard commented May 23, 2016

Since JSX is a global namespace, it is not possible to use typings declared in a module for its Element or ElementClass.

When writing a module, it would then be convenient to write something like ;

import {MyEltType, JSX}  from 'my-custom-jsx-lib'

var myelt = <div> ...</div>

and have typescript check Element, ElementClass and so on on its local JSX instance.

Also, when not providing any JSX namespace but having a factory that has a return type, the compiler crashes nastily with a

/usr/lib/node_modules/typescript/lib/tsc.js:33078
                throw e;
                ^

TypeError: Cannot read property 'flags' of undefined
    at isRelatedTo (/usr/lib/node_modules/typescript/lib/tsc.js:16013:32)
    at checkTypeRelatedTo (/usr/lib/node_modules/typescript/lib/tsc.js:15941:26)
    at checkTypeAssignableTo (/usr/lib/node_modules/typescript/lib/tsc.js:15819:20)
    at isTypeAssignableTo (/usr/lib/node_modules/typescript/lib/tsc.js:15813:20)
    at getJsxElementAttributesType (/usr/lib/node_modules/typescript/lib/tsc.js:18463:47)
    at checkJsxOpeningLikeElement (/usr/lib/node_modules/typescript/lib/tsc.js:18564:40)
    at checkJsxSelfClosingElement (/usr/lib/node_modules/typescript/lib/tsc.js:18265:13)
    at checkExpressionWorker (/usr/lib/node_modules/typescript/lib/tsc.js:20439:28)
    at checkExpression (/usr/lib/node_modules/typescript/lib/tsc.js:20349:42)
    at checkExpressionCached (/usr/lib/node_modules/typescript/lib/tsc.js:20310:38)
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 23, 2016
@RyanCavanaugh RyanCavanaugh self-assigned this May 23, 2016
@RyanCavanaugh
Copy link
Member

Regarding the first point, you can now do this using the declare global { syntax.

I'll take a look at the crash

@mhegazy mhegazy added this to the TypeScript 2.0 milestone May 23, 2016
@ceymard
Copy link
Author

ceymard commented May 24, 2016

By doing

declare global {

  namespace JSX {

  }

}

I get an error TS2665: Module augmentation cannot introduce new names in the top level scope.

Is there something I'm not seeing ?

@ceymard
Copy link
Author

ceymard commented May 24, 2016

Does this mean I have to have a .d.ts file declaring the namespace to be able to augment it ?

If so, would that mean that I would be limited to extending the JSX.Element, rather than define it altogether ?

@RyanCavanaugh
Copy link
Member

That error and restriction has been removed

@ceymard
Copy link
Author

ceymard commented May 24, 2016

Should I use a nightly build then ?

By the way I have tried to make a simple reproduction for the mentioned bug, but couldn't. The library that explodes as such is on github and I'd be delighted to show you how to reproduce if you wish.

@RyanCavanaugh
Copy link
Member

Yea, the nightly build should make that error go away.

A github repo for the bug would be great, always happy to look at those.

@ceymard
Copy link
Author

ceymard commented May 25, 2016

I just tested with [email protected] and the crash is no longer present.

There is a way of reproducing with 1.8.10 (I started a branch for this), are you still interested or do you consider this as resolved ?

@RyanCavanaugh
Copy link
Member

If it's fixed in the nightly then it's probably a duplicate of #7286

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels May 25, 2016
@RyanCavanaugh RyanCavanaugh removed their assignment May 25, 2016
@ceymard
Copy link
Author

ceymard commented Jun 1, 2016

Since everything is working fine for me, shouldn't we mark this as closed ?

@ellisonbg
Copy link

We have a large extensible application where different extension authors might use different virtual dom libraries underneath TSX. Right now because JSX is expected to be global that is difficult. Allowing the JSX namespace to be resolved locally in a module would help a ton.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2017

@ellisonbg please see #15386

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants