Title: Feature Request: Expose TS configuration and internals as Types · Issue #50196 · microsoft/TypeScript · GitHub
Open Graph Title: Feature Request: Expose TS configuration and internals as Types · Issue #50196 · microsoft/TypeScript
X Title: Feature Request: Expose TS configuration and internals as Types · Issue #50196 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms expose CompilerOptions types ✅ Viability 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 exi...
Open Graph Description: Suggestion 🔍 Search Terms expose CompilerOptions types ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wou...
X Description: Suggestion 🔍 Search Terms expose CompilerOptions types ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/50196
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature Request: Expose TS configuration and internals as Types","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nexpose CompilerOptions types \r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nI want to suggest that TypeScript would add some global namespace that contains information about the current running setup:\r\n\r\n* TS version (having some `AtLeast` type like I showcase in our \"hacked use cases\" below would be great too, but if we know the major and the minor we can also do that in userland)\r\n* active `compilerOptions` flags - it would be amazing to have some string union `TypeScript.CompilerOptions.ActiveFlags` where we could just do \r\n```ts\r\n'strictNullChecks' extends TypeScript.CompilerOptions.ActiveFlags ? SomeBehaviour : SomeLessSafeFallback\r\n```\r\n* the current `compilerOptions` in general - I don't know why someone would need access to `TypeScript.CompilerOptions.allowSyntheticDefaultImports` but it would feel like a waste not to expose that\r\n\r\n\r\n## 📃 Motivating Example\r\n\r\n```ts\r\ntype MessageFromTypeScript = AtLeast\u003c[TypeScript.Version.Major, TypeScript.Version.Minor], [4, 8]> extends true \r\n? \"it's the future\" \r\n: \"you live in the past\"\r\n\r\nclass MyRecord\u003cT>{\r\n get(index: string): \r\n 'noUncheckedIndexedAccess' extends TypeScript.CompilerOptions.ActiveFlags \r\n ? (T | undefined) \r\n : T\r\n}\r\n```\r\n\r\n## 💻 Use Cases\r\n\r\nAs library authors, we currently have to apply quite a number of hacks to support as many versions of TypeScript and as many different user configurations as possible.\r\n\r\nSome example problems we are facing:\r\n\r\n1. Slight behavioral changes between TS versions. For example, #49307 forced us to change a `{}` to `ACR[T]` - but only in TS versions above 4.8. The fix breaks in older versions.\r\n2. Behavioral changes depending on `tsconfig.json` settings. Imagine a mapped type with functions.\r\n The user specifies `someMethod(action: PayloadAction\u003cstring>): void` and we map that over to a `someMethod(arg: string): void`. \r\n In the case the user specifies `someMethod(action: PayloadAction\u003cstring | undefined>): void`, we map it over to an optional argument in the form `someMethod(arg?: string): void`\r\n Now assume the user has `strictNullChecks: false` in their tsconfig. Suddenly everything falls into the second case. \r\n3. A library might for example also behave differently if the user has `noUncheckedIndexedAccess: true` in their `tsconfig.json`\r\n\r\nThis leads to quite a few weird hacks in library types that might essentially break with every new release.\r\n\r\n1. Could be solved with a `typesVersion`, but honestly: if we can avoid having two complete sets of types, we want to avoid that. We had it for a while for pre-4.1 types and post-4.1 types and maintaining it was a pain. Right now, I've published https://github.com/phryneas/ts-version/ which uses a monstrous `package.json` with typesVersions just to export the current TS Major and Minor. \r\nSo we solve our problem with an additional dependency and \r\n```ts\r\nimport('@phryneas/ts-version').TSVersion.AtLeast\u003c4, 8> extends true\r\n ? ACR[T]\r\n : {}\r\n```\r\nIn the past we used hacks like\r\n```ts\r\nexport type AtLeastTS35\u003cTrue, False> = [True, False][IsUnknown\u003cReturnType\u003c\u003cT>() => T>, 0, 1>]\r\n```\r\nfor that.\r\nFor a while we also had a subfolder with a `package.json` that would only use `typesVersions` on one import in that subfolder. At least we did not have to maintain two complete separate type definitions, but let's say it was not a great developer experience.\r\n2. We check for `strictNullChecks` with \r\n```ts\r\ntype WithStrictNullChecks\u003cTrue, False> = undefined extends boolean ? False : True;\r\n```\r\n3. We are not doing this yet but I'm sure we'd find a way to *somehow* detect `noUncheckedIndexedAccess` as well.\r\n\r\n\r\nAll that said: we have problems and we have solutions. But our solutions feel horrible and wrong. And also, having a package around with 120 different `typesVersions` entries just to detect the current TypeScript version can't really be good for performance.\r\n\r\nI hope you're going to give this at least some consideration :)","author":{"url":"https://github.com/phryneas","@type":"Person","name":"phryneas"},"datePublished":"2022-08-05T13:27:07Z","url":"https://github.com/microsoft/TypeScript/issues/50196"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:bcd1fb8b-58b7-5ff1-dc53-da09fab37b59 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAFA:1F65:11E3870:16B1A13:6B017737 |
| html-safe-nonce | a186dc5d2b0edd366ddbc756487798a1b63e4bb9fb9f23ea889ffc548e924fd6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUZBOjFGNjU6MTFFMzg3MDoxNkIxQTEzOjZCMDE3NzM3IiwidmlzaXRvcl9pZCI6IjY3NzkxNDI4NzI0NjY4MTQ3NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4238cf584a6b71fdcf49784d4a04b80ff4bbfcc1eadcf7232124bd097a1f6da1 |
| hovercard-subject-tag | issue:1329940505 |
| 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/50196/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8806b9a217234b378eaafb6b572fc8170fc7bb49fda56447a9d49bdb5d44fe92/microsoft/TypeScript/issues/50196 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8806b9a217234b378eaafb6b572fc8170fc7bb49fda56447a9d49bdb5d44fe92/microsoft/TypeScript/issues/50196 |
| og:image:alt | Suggestion 🔍 Search Terms expose CompilerOptions types ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wou... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | phryneas |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width