镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Allow pure ECMAScript block #1644

Description

@NN---

Motivation:
TypeScript currently doesn't support all ES6 features and it takes time for supporting them.
Moreover each vendor plays with new ES extensions like ES7 draft and so on.

Proposal:
Allow pure ECMAScript block which is not treated as TypeScript.
It is pretty similar to '_asm' directive in C language.

This allows to use generators of ES6 while TS doesn't support them:

__ecmascript {
  function *foo() {
    yield 1;
  }
}

// Since the block is not parsed by the TS compiler we must declare the function.
declare extern foo() : any;
var it = foo();
var value = it.next();

Or 'const' which is not supported in TS 1.3:

__ecmascript {
  const A = 1;
}

// Declaring 'A' to be used in the code.
// TS compiler doesn't protect us from assigning to 'A' but ES engine does.
declare var A : number;

Activity

  1. NoelAbrahams commented on Jan 12, 2015

    @NoelAbrahams

    What's wrong with just adding an ES6 compiler option? (e.g. #595)

  2. NN--- commented on Jan 12, 2015

    @NN---
    Author

    There is nothing wrong with that but it takes time for implementing the full ES6 spec.
    Also think about future, ES7 would have new features, perhaps new syntax.
    This will allow to mix in one file TS and new ES syntax.
    Currently I have to split my code into consts.js for 'const' syntax and myfile.ts for the rest.

  3. NoelAbrahams commented on Jan 12, 2015

    @NoelAbrahams

    I believe that is the idea see #904. It's not about down-level emit.

  4. NN--- commented on Jan 12, 2015

    @NN---
    Author

    As I said it is not only about const or ES6, it is about any future feature to be used natively in TS without adding additional ES files until TS supports it natively.

  5. saschanaz commented on Jan 12, 2015

    @saschanaz
    Contributor

    One may use async/await in that block and give the compiled result to other converter for ES6. 👍

  6. danquirk commented on Jan 12, 2015

    @danquirk
    Member

    Curious what platforms you're targeting where you're using some of these features already given the sparse support I see so far (http://kangax.github.io/compat-table/es6/).

  7. jods4 commented on Jan 12, 2015

    @jods4

    Dan Quirk (@danquirk): no need for platform support, you can transpile your code, e.g. using 6to5. Those features make your dev experience much better. BTW, IE12 is expected to come out early this year and have complete ES6 support. You can expect the other browsers to follow suit.

    I think I opened #1641 for the same ground reason: TS is great but its lack of ES6 features is getting more and more frustrating :( For big new projects I'm torn between TS and ES6/7...

  8. basarat commented on Jan 13, 2015

    @basarat
    Contributor

    👍 e.g. the following

    __ecmascript {
      function *foo() {
        yield 1;
      }
    }

    would emit the following and not do any type checking

      function *foo() {
        yield 1;
      }

    It can be useful for the use cases already mentioned (i.e. using an alternative ES6 transpiler) plus also helps with bringing in JS codebases into a TypeScript file without seeing all the red lines.

  9. NoelAbrahams commented on Jan 13, 2015

    @NoelAbrahams

    I fear this may leave the codebase open to abuse:

    __ecmascript {
    
     // Can't be bothered to annotate
    }

    When compilation is permitted to have holes cut out, confidence in the system will be undermined.

  10. jods4 commented on Jan 13, 2015

    @jods4

    I agree opening holes like that is denying a lot of benefits of having a complete Typescript codebase with "no implicit any" option activated. Good bye safe refactorings and find all references.

    Moreover it's ugly and clearly a temporary hack until the language improves (but then you're stuck with it).

    As much as I want TS to improve quickly towards ES6 compliance, this doesn't seem like a good idea to me.

  11. RyanCavanaugh commented on Jan 13, 2015

    @RyanCavanaugh
    Member

    ES6 features are coming online in the compiler pretty quickly (stay tuned). ES7 is supposedly going to have less new syntax than ES6 so I don't think this is going to be as much of an issue going forward. ES8, well, who knows, but we can see what that looks like when it gets closer.

  12. jods4 commented on Jan 13, 2015

    @jods4

    ES6 features are coming online in the compiler pretty quickly (stay tuned)

    This is awesome news! Thanks.

  13. danquirk commented on Jan 13, 2015

    @danquirk
    Member
  14. NN--- commented on Jan 15, 2015

    @NN---
    Author

    We are waiting.
    The most needed feature for me are 'const' and 'let' .

  15. danquirk commented on Jan 15, 2015

    @danquirk
    Member

    let and const were added in October #904

  16. NN--- commented on Jan 16, 2015

    @NN---
    Author

    But still not in the official installer and VS integration :)

  17. danquirk commented on Jan 16, 2015

    @danquirk
    Member

    1.4 was released today and it's definitely in there.

  18. mhegazy commented on Jan 16, 2015

    @mhegazy
    Contributor

    NN (@NN---) you would need to add lib.es6.d.ts explicitly to your project, see #1696 for details.

  19. NN--- commented on Jan 17, 2015

    @NN---
    Author

    Dan Quirk (@danquirk) That's great !
    There are no const enums for strings still, right ?

  20. DanielRosenwasser commented on Jan 19, 2015

    @DanielRosenwasser
    Member

    NN (@NN---) that's correct.

  21. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions