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

as operator doesn't work correctly with CommonJS #5245

Closed
oryol opened this issue Oct 13, 2015 · 1 comment
Closed

as operator doesn't work correctly with CommonJS #5245

oryol opened this issue Oct 13, 2015 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@oryol
Copy link

oryol commented Oct 13, 2015

I'm trying to use generic class in the TSX file and generic class is implemented in the another module. I have tried to use code like in the #3960 (comment) but it doesn't work because old style casting is not allowed in the TSX file. Ok, I have changed it to the as operator but it emits incorrect code. It refers to the generic class directly without using require.

After that I have created simple example without JSX which demonstrates that old style casting and as operator are different.

File generic.ts: export default class Generic<T> {}

File sample.ts:

import Generic from "./generic";
type Ctor = new() => Generic<number>;
const x = Generic as Ctor;

Compile tsc sample.ts --module commonjs. Result:

var generic_1 = require("./generic");
var x = Generic;

As you see generic_1 variable isn't used at all and undeclared Generic variable is assigned to x.

Workaround:

import Generic from "./generic";
type Ctor = new () => Generic<number>;
const x = Generic;
const y = x as Ctor;

Also please note that using old-style casting also helps:

import Generic from "./generic";
type Ctor = new() => Generic<number>;
const x = <Ctor>Generic;

So it looks like it is the problem in the as operator only.

@RyanCavanaugh
Copy link
Member

Duplicate of #4832

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 13, 2015
@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

2 participants