-
-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I tried to make all these types globally available in my project, but I failed. #470
Comments
Global types are generally discouraged and I don't intend to change anything to support them here. However, you could maybe do this yourself by importing the types using |
Can I just ask, why are global types discouraged? |
Global types are bad in packages as they can affect types elsewhere in your dependency tree. Exposing types yourself in your own global type definition in a non-package project is totally fine though. |
I'm sorry, but either what you said is incorrect or I don't quite understand what you just said. If you let TS generate a And you say:
Which indeed I'm already doing for a few types, like so: // This is in my own global.d.ts file
declare type Class<T, Arguments extends unknown[] = any[]> =
import("type-fest").Class<T, Arguments> And that works, but I'm simply asking, could you make it easier for me to do exactly the same thing, but then for all your types, so that I don't have repeat that line of code for every single type your library contains. For example, Vue 3 has an experimental feature that they call It's just so convenient, I don't see why you wouldn't give developers the option? If a developer thinks it's bad practice then they can simply choose to not add I mean, TS itself has a bunch of globally available utility types. And aren't we all glad that we don't have to write I apologize if the tone of my comment sounds antagonizing. It's just that I really don't understand your reasoning about why it's bad practice, and I don't see how adding this feature could negatively affect people who don't want to use the feature, and I would soooooo love to be able to just get this convenience. |
// types/type-fest.d.ts
import { IsEqual } from 'type-fest';
declare global {
export namespace TypeFest {
export type * from 'type-fest';
import('type-fest');
}
export namespace T {
export type * from 'type-fest';
import('type-fest');
export type ConditionalExactKeys<Base, Condition> = NonNullable<
// Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
{
// Map through all the keys of the given base type.
// [Key in keyof Base]: [Condition] extends [Base[Key]] // Pick only keys with types extending the given `Condition` type.
[Key in keyof Base]: IsEqual<Base[Key], Condition> extends true // Pick only keys with types exact equal the given `Condition` type.
? // Retain this key since the condition passes.
Key
: // Discard this key since the condition fails.
never;
// Convert the produced object into a union type of the keys which passed the conditional test.
}[keyof Base]
>;
}
}
export {}; // tsconfig.json
{
"include": [
"types/type-fest.d.ts",
]
}
useage// type T1 = "a" | "b" | "b.c"
type T1 = T.Paths<{ a: string; b: { c: number } }>;
// type T2 = "fooBar"
type T2 = TypeFest.CamelCase<'foo-bar'>;
type Foo = {
a: number;
b?: number;
c: 1 | 2 | 3;
};
// type T3 = "c" | "a" | "b"
type T3 = T.ConditionalKeys<Foo, number | undefined>;
// type T4 = "b"
type T4 = T.ConditionalExactKeys<Foo, number | undefined>; |
In my
src
folder I have aglobal.d.ts
file, which already starts with the line/// <reference types="vue/macros-global" />
and that works fine. So I thought I could just add another line/// <reference types="type-fest" />
. But unfortunately that does not do the trick.Is it because your
index.d.ts
usesexport
statements? If so, would you be willing to make another file that exports the types in a way that I can "import" them in myglobal.d.ts
?Upvote & Fund
The text was updated successfully, but these errors were encountered: