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

type alias does not work with classes #5014

Closed
ghost opened this issue Sep 29, 2015 · 8 comments
Closed

type alias does not work with classes #5014

ghost opened this issue Sep 29, 2015 · 8 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ghost
Copy link

ghost commented Sep 29, 2015

I defined a class:

    export class TCommand<TSender, TParam, TThis> 
    {
    }

and created an alias for the closed type:

 export type Command = TCommand<any, any, any>;

now when i try:

 export class NextCommand extends Command {
 }

the compiler says: Error 1 Cannot find name 'Command'

but i can write

  var cmd: Command = null;

without errors,

Expected: to allow using this type alias for inheritance purposes.

@ghost
Copy link
Author

ghost commented Sep 29, 2015

  var cmd = new MOD.mvvm.Command();

Does not work too. The same Error!

@mhegazy mhegazy changed the title type synonym does not work with classes type alias does not work with classes Sep 29, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2015

The problem here is extends needs a value to use at run time. The generated code will look something like:

var NextCommand = (function (_super) {
    __extends(NextCommand, _super);
    function NextCommand() {
        _super.apply(this, arguments);
    }
    return NextCommand;
})(Command);  /// notice Command is not defined.

So you can only extend from value. the type alias Command is just an alias, another name you can use in the TS code to refer to type NextCommand and does not exist in the generated code, this is why you can use it in a type position, e.g. var cmd: Command, as this has no implication at run-time.

you can achieve what you want by creating a new class, something like:

class Command extends TCommand<any, any, any> { }

// then this works
export class NextCommand extends Command {
}

// and this:
var cmd = new MOD.mvvm.Command();

for more information about what is value vs. what is type see: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Declaration%20Merging.md#basic-concepts

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Sep 29, 2015
@mhegazy mhegazy closed this as completed Sep 29, 2015
@ghost
Copy link
Author

ghost commented Sep 29, 2015

@mhegazy
Thanks for yor reply. The typescript should create a true alias.
That means instead of "Command" put in that place TCommand - because "Command" is only an alias for TCommand. We need replacements, not a new definition "Command" in the compiler's output.
It should be:
var NextCommand = (function (_super) {
__extends(NextCommand, _super);
function NextCommand() {
_super.apply(this, arguments);
}
return NextCommand;
})(TCommand);

and

 var cmd = new MOD.mvvm.Command();

should be in JavaScript

   var cmd = new MOD.mvvm.TCommand();

P.S. - please, reopen the bug and fix this in new version

@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2015

if you want an alias for both value and type use import:

export import Command = MOD.mvvm.TCommand;

for your request, see #2559

@ghost
Copy link
Author

ghost commented Sep 29, 2015

@mhegazy
Is this would work too?

       export import Command = MOD.mvvm.TCommand <>

@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2015

yes.

@ghost
Copy link
Author

ghost commented Sep 29, 2015

@mhegazy
No it does not work with generic parameters in TCommand

Better to make the alias work properly

@mhegazy
Copy link
Contributor

mhegazy commented Sep 29, 2015

No it does not work with generic parameters in TCommand

Please see #2559 .

@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

1 participant