-
Notifications
You must be signed in to change notification settings - Fork 2k
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
@uppy/core: add generic to getPlugin
#5247
Conversation
Diff output filesNo diff |
wouldn't it be more elegant if we return the correct type based on the string value passed to from chatgpt: In TypeScript, you can use conditional types to return a different type based on a constant string value passed as an argument to a function. Here's an example: type TypeMap = {
type1: number;
type2: string;
type3: boolean;
};
function getValue<T extends keyof TypeMap>(type: T): TypeMap[T] {
let value: TypeMap[T];
if (type === 'type1') {
value = 123 as TypeMap[T];
} else if (type === 'type2') {
value = 'hello' as TypeMap[T];
} else if (type === 'type3') {
value = true as TypeMap[T];
}
return value;
}
let a = getValue('type1'); // a is of type number
let b = getValue('type2'); // b is of type string
let c = getValue('type3'); // c is of type boolean In this example, |
I don't think that's possible, the |
We could also have a static method e.g. |
Indeed not possible afaik. There might be one small chance at getting something to work like that, which is this todo I wrote here: uppy/packages/@uppy/core/src/Uppy.ts Lines 147 to 149 in c5d7808
Something along these lines, in every plugin: declare module '@uppy/core' {
export interface Plugins<M extends Meta, B extends Body> {
'Transloadit': Transloadit<M, B>
}
} |
but won't users 99% of the time write |
that could work alternatively we could implement a new static API for accessing our built in plugins, like uppy.transloadit?: TransloaditPlugin
uppy.dropbox?: DropboxPlugin
// and so on or uppy.plugins.transloadit?: TransloaditPlugin
uppy.plugins.dropbox?: DropboxPlugin
// and so on |
@mifi what I meant is that users can provide a different id, and we can't really predict what those are going to be (heck they could use the string uppy/packages/@uppy/aws-s3/src/index.ts Line 339 in 35525ae
|
maybe we should prevent people from using the string then we can predict the return type in most cases, but of course if they have a custom plugin our return types can be |
The Let's not overthink it, this PR is already an improvement, no user has complained about this. |
* 4.x: Renames & `eslint-disable react/require-default-props` removal (#5251) coalesce options `bucket` and `getKey` (#5169) @uppy/aws-s3: add `endpoint` option (#5173) @uppy/locales: fix `fa_IR` export (#5241) improve companion logging (#5250) Release: uppy@4.0.0-beta.11 (#5243) @uppy/core: add generic to `getPlugin` (#5247) docs: add 4.x migration guide (#5206) @uppy/transloadit: also fix outdated assembly transloadit:result (#5246) docs - fix typo in the url @uppy/core: set default for Body generic (#5244) Release: uppy@3.26.1 (#5242) docs: clarify assemblyOptions for @uppy/transloadit (#5226) meta: Improve aws-node example readme (#4753) @uppy/react: remove `react:` prefix from `id` & allow `id` as a prop (#5228) Added translation string (it_IT) (#5237) docs: correct allowedMetaFields (#5227) @uppy/transloadit: fix transloadit:result event (#5231) docs: remove `extraData` note from migration guide (#5219) @uppy/provider-views: fix wrong font for files (#5234)
It's a common use case to
getPlugin
and access something on it. Currently it's a bit awkward:This PR reintroduces the generic on
getPlugin
, so it's an improvement but also backwards compatibility with 3.x:Unfortunately you must pass the generics, which should be optional but I don't know how to achieve that.