You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to suggest that TypeScript would add some global namespace that contains information about the current running setup:
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)
active compilerOptions flags - it would be amazing to have some string union TypeScript.CompilerOptions.ActiveFlags where we could just do
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
π Motivating Example
typeMessageFromTypeScript=AtLeast<[TypeScript.Version.Major,TypeScript.Version.Minor],[4,8]>extendstrue
? "it's the future"
: "you live in the past"classMyRecord<T>{get(index: string):
'noUncheckedIndexedAccess'extendsTypeScript.CompilerOptions.ActiveFlags
? (T|undefined)
: T}
π» 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.
Behavioral changes depending on tsconfig.json settings. Imagine a mapped type with functions.
The user specifies someMethod(action: PayloadAction<string>): void and we map that over to a someMethod(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 form someMethod(arg?: string): void
Now assume the user has strictNullChecks: false in their tsconfig. Suddenly everything falls into the second case.
A library might for example also behave differently if the user has noUncheckedIndexedAccess: true in their tsconfig.json
This leads to quite a few weird hacks in library types that might essentially break with every new release.
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.
So we solve our problem with an additional dependency and
for that.
For 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.
2. We check for strictNullChecks with
We are not doing this yet but I'm sure we'd find a way to somehow detect noUncheckedIndexedAccess 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 :)
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:
AtLeasttype 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)compilerOptionsflags - it would be amazing to have some string unionTypeScript.CompilerOptions.ActiveFlagswhere we could just docompilerOptionsin general - I don't know why someone would need access toTypeScript.CompilerOptions.allowSyntheticDefaultImportsbut 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.jsonsettings. Imagine a mapped type with functions.The user specifies
someMethod(action: PayloadAction<string>): voidand 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): voidNow assume the user has
strictNullChecks: falsein their tsconfig. Suddenly everything falls into the second case.noUncheckedIndexedAccess: truein theirtsconfig.jsonThis 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.jsonwith 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.jsonthat would only usetypesVersionson 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
strictNullCheckswithnoUncheckedIndexedAccessas 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
typesVersionsentries 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 :)