Skip to content

'--alwaysStrict' option should not 'use strict' declaration if setting with module=es2015 #11806

Closed

Description

Env

  • TypeScript Version 2.1.0-dev.20161023
  • with enable --alwaysStrict & --module=es2015 option

Code

Compile the below code with the above options.

export const a = 1;

Actual behavior

"use strict";
export var a = 1;

Expected behavior

I think "use strict" declareation is needless for ES2015 module because "Module code is always strict mode code" by the spec.

Activity

  1. tetsuharuohzeki commented on Oct 24, 2016

    @tetsuharuohzeki
    Author

    relate: #10758

  2. mhegazy commented on Oct 24, 2016

    @mhegazy
    Contributor

    I agree it is invalid with --target ES2015; but the above sample is for --module ES2015, which i do not see why it is invalid. the module code has been parsed and transformed, and the "use strict" is part of this transformation.

  3. tetsuharuohzeki commented on Oct 25, 2016

    @tetsuharuohzeki
    Author

    import/export declarations introduced by es2015 is only be parsed in parsing them as Module. Paradoxically, we can regards as Module as the source file which includes import/export declarations and is parsed as valid ecma262 code.

  4. kitsonk commented on Oct 25, 2016

    @kitsonk
    Contributor

    Modules in the ES2015 module format do not have to be loaded by the native module loader (since there is yet to be a production ready one). Having a valid module format does not always imply the code will be interpreted in strict mode. While SystemJS will load modules in strict mode when their source module format is ES2015, other bundlers may or may not do the same.

    Since there is no issue extra emits of the prolog, I would say it is still best to emit code targeted at ES5 to provide the "use strict"; prolog irrespective of the module format. It minimises any surprises.

  5. modified the milestones: TypeScript 2.1, on Oct 27, 2016
  6. aluanhaddad commented on Oct 30, 2016

    @aluanhaddad
    Contributor

    I am definitely in favor of leaving this as is.

  7. slawomir commented on Oct 30, 2016

    @slawomir
    Contributor

    Specification mentioned by saneyuki stays that modules are interpreted in strict mode. Specification means ES6 modules, so following conditions must be met to make TypeScript generate ES6 module:

    • --target es2015
    • --module es2015

    --module cannot be set to for example commonjs, because then result Javascript will not be ES6 module, and strict mode will not be assured by default in runtime.

    On the other hand adding "use strict" in generated ES6 module should not cause any problems. In that case skipping use strict for ES6 modules would be an optimisation to result code.

  8. locked and limited conversation to collaborators on Jun 19, 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

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions