microsoft/TypeScript

Enable "useDefineForClassFields" in tsc --init

robpalme opened this issue · 4 comments

Search Terms

useDefineForClassFields, tsc --init, tsconfig

Suggestion

tsc --init's generated tsconfig.json should include "useDefineForClassFields": true

Use Cases

All major web and non-web engines now ship Define-style semantics.

So the safest approach is to ensure new TypeScript projects use standard web-compatible behavior.

Examples

It looks like we just need to add one line here. I am happy to send a PR.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

I think this is a good idea. @ahejlsberg @DanielRosenwasser thoughts?

Three concerns we should discuss:

  1. The emit is bulky for all targets <ESNext.
  2. As of 4.0, we have errors around places where [[Set]] and [[Define]] differences are observable.
  3. [[Define]] semantics are at stage 3, so strict standards compliance isn't really a motivator yet.

We have existing features with non-compliant emit, iteration for example, and errors to prevent the non-compliance from being observed, so there's precedence for prioritising good emit over standards compliance.

If we had multiple --init options, this would be the perfect addition to a "latest" init.

orta commented

I added it to the recommended tsconfig base: tsconfig/bases#15

From discussion in the design meeting: We decided not to do this until [[Define]] semantics ship in some ES20xx spec, and that ES20xx becomes the default target in tsc --init.

Currently the default target is ES5, which we need to rethink -- in my opinion, ES2015 at least should be the default.