Title: Proposal: Granular Targeting · Issue #4692 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Granular Targeting · Issue #4692 · microsoft/TypeScript
X Title: Proposal: Granular Targeting · Issue #4692 · microsoft/TypeScript
Description: This proposal is based on a working implementation at: https://github.com/yortus/TypeScript/tree/granular-targeting To try it out, clone it or install it with npm install yortus-typescript Problem Scenario The TypeScript compiler accepts...
Open Graph Description: This proposal is based on a working implementation at: https://github.com/yortus/TypeScript/tree/granular-targeting To try it out, clone it or install it with npm install yortus-typescript Problem ...
X Description: This proposal is based on a working implementation at: https://github.com/yortus/TypeScript/tree/granular-targeting To try it out, clone it or install it with npm install yortus-typescript Problem ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/4692
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Granular Targeting","articleBody":"This proposal is based on a working implementation at:\nhttps://github.com/yortus/TypeScript/tree/granular-targeting\nTo try it out, clone it or install it with `npm install yortus-typescript`\n## Problem Scenario\n\nThe TypeScript compiler accepts a single `target` option of either `ES3`, `ES5` or `ES6`. However, most realistic target environments support a mixture or ES5 and ES6, and even ES7, often known in advance (e.g. when targeting Node.js, and/or using polyfills).\n\nUsing TypeScript with target environments with mixed ES5/5/7 support presents some challenges, many of which have been discussed in other issues. E.g.:\n- (#4389) Support compile targets between ES5 and ES6\n- (#4168) Normalize our lib files by compiler settings\n- (#3215) New APIs added to lib.d.ts may break client codes. Allow duplicated members in interfaces? Make lib.d.ts overridable?\n- (#3005) Using ES6 type default library when targetting ES5 output\n- (#2695) for-of does not work with DOM collections when target is ES6\n- (somewhat related: (#2481) Create DOM-Level specific dom-version.d.ts)\n\nIn summary:\n- The default lib either includes all ES6 types and properties, or none of them.\n- Specifying `--noLib` and/or manually maintaining `lib.b.ts` files brings other problems:\n - separate core typings are a burden to maintain.\n - problems of missing symbols and clashing symbols.\n - burden of manually tracking fixes and additions made in the default libs.\n- Targeting ES5 as the 'lowest common denominator' means some language features known to be supported cannot be used (eg generators in Node.js).\n- Targeting ES6 (e.g. to take advantage of Node.js' support for many ES6 features) leads to further complications:\n - \u003cdel\u003eCommonJS modules won't compile, even though that's the only module system Node supports.\u003c/del\u003e (fixed by #4811)\n - The compiler emits ES6 even for features that are known _not_ to be supported, which would fail at runtime.\n - Adding babel.js to the build pipeline adds complexity.\n## Workarounds\n#### To achieve mixed ES5/ES6 core typings:\n- specify `--target ES5` and selectively add ES6 typings in separately maintained files (eg from DefinitelyTyped).\n- specify `--target ES6` and be careful to avoid referencing unsupported ES6 features (the compiler won't issue any errors).\n- specify `--noLib` and manually maintain custom core typings in your own project.\n#### To use ES6 features supported by the target platform\n- specify `--target ES5` and (a) accept that things will be down-level emitted, and (b) don't use features with no down-level emit yet (ie generators).\n- specify `--target ES6` and (a) \u003cdel\u003econvert everything from CommonJS to ES6 modules\u003c/del\u003e (fixed by #4811), (b) add babel.js to the build pipeline, and (c) configure babel.js to do either pass-through or down-level emit on a feature-by-feature basis.\n## Proposed Solution\n\nThis proposal consists of two parts:\n\n\u003cdel\u003e1. Support for conditional compilation using `#if` and `#endif` directives, so that a single default lib can offer fine-grained typings tailored to a mixed ES3/5/6/7 target environment.\u003c/del\u003e\n\n\n\u003cdel\u003eThe conditional compilation part is detailed in a separate proposal (#4691) with its own [working implementation](https://github.com/yortus/TypeScript/tree/preprocessor-directives).\u003c/del\u003e\n\n\n**1. A mechanism allowing the default lib to offer fine-grained typings tailored to a mixed ES3/5/6/7 target environment.**\n\nThis is really an internal compiler detail, so the mechanism is open to debate. It just has to match the granularity supported by the new compiler options below.\n\nThe [working implementation](https://github.com/yortus/TypeScript/tree/granular-targeting) uses `#if...#endif` conditional compilation proposed in #4691. But this is overkill for this use case and [seems unlikely](https://github.com/Microsoft/TypeScript/issues/4691#issuecomment-138678473) to be considered.\n\nSeveral other mechanisms have been discussed (summarized [here](https://github.com/Microsoft/TypeScript/issues/4692#issuecomment-138750104)).\n\n**2. Support for additional compiler options allowing the target environment to be described on a feature-by-feature basis.**\n\nUnder this proposal, the `target` option remains, but is now interpreted as the 'baseline' target, determining which features the target supports by default. For instance, ES6 symbols and generators are supported by default if `target` is set to `ES6` or higher.\n\nThe additional compiler options have the form `targetHasXYZ`, where `XYZ` designates a feature. These options are used to override the target for a particular language feature. They instruct the compiler that the target environment explicitly does or does not support a particular feature, regardless of what the `target` option otherwise imples.\n\nThe [working implementation](https://github.com/yortus/TypeScript/tree/granular-targeting) currently supports the following additional compiler options (all boolean):\n- `targetHasArrowFunctions`: specify whether the target supports ES6 `() =\u003e {...}` syntax\n- `targetHasBlockScoping`: specify whether the target supports ES6 `let` and `const`\n- `targetHasForOf`: specify whether the target supports ES6 `for..of` syntax\n- `targetHasGenerators`: specify whether the target supports ES6 generators\n- `targetHasIterables`: specify whether the target supports ES6 iterables and iterators\n- `targetHasModules`: specify whether the target supports ES6 modules\n- `targetHasPromises`: specify whether the target supports ES6 promises\n- `targetHasSymbols`: specify whether the target supports ES6 symbols\n\nThese options work both on the command line and in `tsconfig.json` files.\n## Example `tsconfig.json` Files and their Behaviour\n#### A.\n\n``` json\n{\n \"target\": \"es6\",\n \"targetHasModules\": false,\n \"targetHasBlockScoping\": false,\n \"module\": \"commonjs\"\n}\n```\n\nEmits ES6 JavaScript, except with CommonJS module syntax, and with `let`/`const` down-leveled to `var`. This might match a Node.js environment.\n#### B.\n\n``` json\n{\n \"target\": \"es5\",\n \"targetHasSymbols\": true\n}\n```\n\nEmits ES5 JavaScript, except with Symbol references emitted as-is, and with full type support for well-known symbols from the default lib.\n#### C.\n\n``` json\n{\n \"target\": \"es5\",\n \"targetHasPromises\": true\n}\n```\n\nEmits ES5 JavaScript, except with full type support for ES6 promises from the default lib. This would work in an ES5 environment with a native or polyfilled `Promise` object.\n## Backward Compatibility, Design Impact, Performance, etc\n- There is no impact on existing TypeScript projects. The additional options and preprocessor directives only modify the compiler's behaviour if they are explicitly used.\n- The preprocessor directives `#if` and `#endif` add new language syntax. No existing language features are affected.\n- There is negligable impact on compiler performance.\n- Only one default lib is needed (`lib.es6.d.ts`). It contains many conditionally compiled sections (ie with `#if` and `#endif`)\n## Remaining Work and Questions\n- Support compiler options for more target features, e.g.:\n - template strings\n - classes\n - `Map`/`Set`/`WeakMap`/`WeakSet`\n - binary and octal literals\n - destructuring\n - default, rest, and optional parameters\n- How granular could/should targets be? Feature support is naturally hierarchical. E.g. block scoping may be separated into (a) `let`, (b) `const` and (c) block-level function declaration. This is true of most features and their realistic implementations (the [Kangax ES6 compatibility table](https://kangax.github.io/compat-table/es6/) has a three-level hierarchy down the left side).\n","author":{"url":"https://github.com/yortus","@type":"Person","name":"yortus"},"datePublished":"2015-09-08T15:54:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":51},"url":"https://github.com/4692/TypeScript/issues/4692"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:443e4d49-1d0c-3cb2-7cef-402c6b191e73 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C3A:2846D4:991C5B:D332CC:6A624A05 |
| html-safe-nonce | 7b344b2a349436110699c9bd552b75f475fcd9f823e5332228cd11ac4b47a86c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzNBOjI4NDZENDo5OTFDNUI6RDMzMkNDOjZBNjI0QTA1IiwidmlzaXRvcl9pZCI6Ijc1ODk0Mjg5MDI4NDg3NzY3MDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3cff4f0a0c4e8528a7d02bd60a9cdfa920f9bf968db11e828c82f1f4db5e9ab5 |
| hovercard-subject-tag | issue:105411159 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/4692/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d45f165aa8c0fb395cfb1cc4c144f9b4ed7c23c8e9e3ee38439275bb2b06ec6/microsoft/TypeScript/issues/4692 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d45f165aa8c0fb395cfb1cc4c144f9b4ed7c23c8e9e3ee38439275bb2b06ec6/microsoft/TypeScript/issues/4692 |
| og:image:alt | This proposal is based on a working implementation at: https://github.com/yortus/TypeScript/tree/granular-targeting To try it out, clone it or install it with npm install yortus-typescript Problem ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yortus |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5d6ba65d73ecc4e3394fe318d2b2f98e6f8eed4878b5421b938e20d30bde267b |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2dadc56fd5989b76a8ae7304e3aa56d0b485e5dc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width