Skip to content

--alwaysStrict not ... always strict #15041

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
jeremyjh opened this issue Apr 6, 2017 · 7 comments
Closed

--alwaysStrict not ... always strict #15041

jeremyjh opened this issue Apr 6, 2017 · 7 comments

Comments

@jeremyjh
Copy link

jeremyjh commented Apr 6, 2017

TypeScript Version: 2.2.2

Code

In a React Native project:

import React from 'react';

let thing = {a: "first"};
Object.freeze(thing);
thing.a = "second";

tsconfig.json

"compilerOptions":{  
   "target":"es2015",
   "module":"es2015",
   "jsx":"react",
   "outDir":"dist",
   "rootDir":"App",
   "allowSyntheticDefaultImports":true,
   "noImplicitAny":true,
   "strictNullChecks":true,
   "alwaysStrict":true,
   "noUnusedLocals":true,
   "experimentalDecorators":true,
   "preserveConstEnums":true,
   "allowJs":true,
   "moduleResolution":"node",
   "types":[  
      "native-base",
      "redux"
   ],
   "sourceMap":true
},

Expected behavior:
A run-time exception, because alwaysStrict: true.

Actual behavior:
Mutation is silently discarded unless "use strict;" is added to the top of the .ts file.

In #11806 it was identified that "use strict;" is superfluous when module=es2015.

I am using module=es2015 in a React Native and would like "use strict" to be applied to all of my files, but I'm not sure how to do this now. Setting module to commonjs will bring back "use strict" but breaks RN. Setting target=es5 does not enable "use strict";.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Apr 6, 2017

Setting target=es5 does not enable "use strict";.

This definitely should work. It is also completely forwards compatible, valid ES2015 to emit a potentially redundant "use strict".

@jeremyjh
Copy link
Author

jeremyjh commented Apr 6, 2017

It looks like the real culprit is the babel preset used in React Native's packager. This can be overrode in .babelrc. This is why RN is not behaving in spec I think.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Apr 6, 2017

@jeremyjh perhaps, but as you say, tsc emits non strict code using your configuration but under --target es5. That is a problem.

Update:
I've changed my mind about this.

An ES Module aware runtime must ensure that all ES Modules are executed in strict mode if it wants to say that it directly supports ES Module syntax.

SystemJS does just that.
It emits a "use strict" directive prologue in its wrapper for modules in packages with metadata specifying format: "esm".

@jeremyjh
Copy link
Author

jeremyjh commented Apr 8, 2017

I'm going to close this; it is not really a Typescript issue, even though Typescript used to support a nice workaround for it. I still actually have not found any workaround other than putting "use strict"; at the top of every module file myself. Building my own .babelrc config with

    ["transform-es2015-modules-commonjs", { strict: true, allowTopLevelThis: true }],

Did not resolve the issue for some reason. But I think its more properly a RN issue.

@jeremyjh jeremyjh closed this as completed Apr 8, 2017
@aluanhaddad
Copy link
Contributor

Perhaps link the issue that you file with React Native.

@jeremyjh
Copy link
Author

jeremyjh commented Apr 9, 2017

@aluanhaddad Actually I found they already had one - facebook/react-native#5316 . Basically they can't fix it, too many libraries out there won't compile under strict and all of your project's node_modules will get processed by the packager.

I did get an override to work locally in project, and found I also had a couple of dependencies that don't support strict. So its notations at the top of all my files.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Apr 9, 2017

That's frustrating, strict mode should be used in almost every situation, both for security and performance reasons.
Regardless, thank you for linking the issue.

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants