-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? # to your account
Feature Request: Expose TS configuration and internals as Types #50196
Comments
I've pitched something like this internally a few times and gotten a lukewarm reception. I think the key is figuring out some way to really scope this down to something that isn't likely to paint us into a corner in the future. For example, today type Foo = {
true: something,
false: somethingElse
}[CompilerOptions['noUncheckedIndexAccess'] then we don't have any way to add new options without breaking this code -- but breaking less code in forward versioning is one of the main goals of this proposal in the first place. |
This specific example wouldn't work anyway since TS doesn't let you index a type with the I'm curious whether it makes sense to expose all compiler options, or a very limited subset. I'm also curious how best to handle something akin to |
Currently my userspace implementation looks like this: Tbh, I'd be perfectly fine with leaving this as an user space implementation. It doesn't satisfy all of Semver, it just works for major-minor-combinations that are likely to ever be used by TypeScript - and once TS ever ships a 4.10 or a 10.0, I'll have to adjust it a bit. The painful-in-userland part is to have the |
I think the feature I suggest here is niche enough to be used only in very specific use cases, and probably only ever in libraries. Something like this could for example be prefixed with |
Or "experimental", like the decorator support. |
Hate to do the "+1" routine, but I'm in another situation where this would be extremely useful. In #50831 (comment) , a change in TS 4.9 broke Reselect (boo!), and specifically a We already ship one entire other set of typedefs - the old ginormous 15-overloads-per-function typedefs that were in Reselect up through 4.0, before we completely rewrote our types in 4.1 to add do a much better job of inference (which is when we added this But, 4.7 is far too new for us to require as a baseline, so I've got to ship a package that works with, realistically, 4.2 and above. So how do I get users who are on 4.2-4.6 to use the existing Well, as Lenz pointed out above, you can pull some stupid hacks with build setup:
That way during dev TS just imports the type as normal, but the built version sees that This is, frankly, some ridiculous shenanigans :) If there was some way to declare in the code itself "hey, if this is TS 4.7 or greater use implementation A, else use implementation B", this would be much simpler. |
I feel like there are two things being discussed here - they are highly related but at the same time risks associated with both of them are very different. It feels that there is a reasonable pushback to exposing compiler options (although I also think that there are reasons for libraries to access this kind of information). This one is probably so bikesheddable that it won't happen any time soon (let's continue discussing this though!). On the other hand, exposing a TS version somehow is way less controversial (that doesn't mean it's not controversial at all π ). How likely this one is to happen? If this feature request would get approved then I could take a stab at implementing this. That being said - this won't really help @markerikson right now as this wouldn't be backported to older TS versions. It could make it easier to select the implementation of the types in the future though. For the time being... I don't feel like |
I think at that point we will end up with some kind of templating engine that creates multiple different Nonetheless, it's something that would help a lot - is there any progress or decision on this? |
Yeah, please let's continue discussing this and not let the discussion die here! Everything we can come up with here will be a 100 times less prone to breakage than trying to detect enabled features with whacky TS types in code. |
Adding my support for this - would be great for library authors and users. In the meantime, I've published a tiny package called Would love for this to become a supported feature by Typescript, as honestly the package workaround is a little gross π |
Suggestion
π Search Terms
expose CompilerOptions types
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I want to suggest that TypeScript would add some global namespace that contains information about the current running setup:
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)compilerOptions
flags - it would be amazing to have some string unionTypeScript.CompilerOptions.ActiveFlags
where we could just docompilerOptions
in general - I don't know why someone would need access toTypeScript.CompilerOptions.allowSyntheticDefaultImports
but it would feel like a waste not to expose thatπ Motivating Example
π» Use Cases
As 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.
Some example problems we are facing:
{}
toACR[T]
- but only in TS versions above 4.8. The fix breaks in older versions.tsconfig.json
settings. Imagine a mapped type with functions.The user specifies
someMethod(action: PayloadAction<string>): void
and we map that over to asomeMethod(arg: string): void
.In the case the user specifies
someMethod(action: PayloadAction<string | undefined>): void
, we map it over to an optional argument in the formsomeMethod(arg?: string): void
Now assume the user has
strictNullChecks: false
in their tsconfig. Suddenly everything falls into the second case.noUncheckedIndexedAccess: true
in theirtsconfig.json
This leads to quite a few weird hacks in library types that might essentially break with every new release.
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 monstrouspackage.json
with typesVersions just to export the current TS Major and Minor.So we solve our problem with an additional dependency and
In the past we used hacks like
for that.
For a while we also had a subfolder with a
package.json
that would only usetypesVersions
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.2. We check for
strictNullChecks
withnoUncheckedIndexedAccess
as well.All 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.I hope you're going to give this at least some consideration :)
The text was updated successfully, but these errors were encountered: