Skip to content
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

Many Typescript types are only accessible by accessing internal files #326

Closed
electrovir opened this issue Sep 11, 2023 · 4 comments · Fixed by #327
Closed

Many Typescript types are only accessible by accessing internal files #326

electrovir opened this issue Sep 11, 2023 · 4 comments · Fixed by #327
Assignees

Comments

@electrovir
Copy link

There are lots of internal types that say "export type" but there's not any way to actually access them from a TypeScript import.

Example:

I'm trying to use the internal ColorTypes type (input to the new Color() constructor). These are the ways I've tried so far that don't work:

  1. default import:
    import Color from 'colorjs.io';
    type ColorTypes = Color.ColorTypes; // does not exist
  2. destructured import:
    import {ColorTypes} from 'colorjs.io'; // does not exist

Workaround

With some type system trickery it is possible to extract this type, but it's quite annoying and requires the use of a huge OverloadParameters helper from this comment:

import Color from 'colorjs.io';

/** Overloaded function type helpers */
type OverloadProps<TOverload> = Pick<TOverload, keyof TOverload>;

type OverloadUnionRecursive<TOverload, TPartialOverload = unknown> = TOverload extends (
    ...args: infer TArgs
) => infer TReturn
    ? TPartialOverload extends TOverload
        ? never
        :
              | OverloadUnionRecursive<
                    TPartialOverload & TOverload,
                    TPartialOverload & ((...args: TArgs) => TReturn) & OverloadProps<TOverload>
                >
              | ((...args: TArgs) => TReturn)
    : never;

type OverloadUnion<TOverload extends (...args: any[]) => any> = Exclude<
    OverloadUnionRecursive<(() => never) & TOverload>,
    TOverload extends () => never ? never : () => never
>;

type OverloadParameters<T extends (...args: any[]) => any> = Parameters<OverloadUnion<T>>;

/** Extracted ColorTypes */
type ColorTypes = Extract<OverloadParameters<(typeof Color)['get']>, [any]>[0];
@electrovir
Copy link
Author

Other import attempts that fail:

  1. import {ColorTypes} from 'colorjs.io/dist/color';
  2. import {ColorTypes} from 'colorjs.io/src/color';

@electrovir
Copy link
Author

electrovir commented Sep 11, 2023

Ah, I found one that works:

import {ColorTypes} from 'colorjs.io/types/src/color';

At least there's a sane way to access them. Still, this seems like an accidental feature; I'd expect to be able to access the types directly from a colorjs.io import.

@electrovir electrovir changed the title How to use the internal TypeScript types? Many Typescript types are only accessible by accessing internal files Sep 11, 2023
@MysteryBlokHed
Copy link
Member

When I was writing the types I guess I didn't think about re-exporting them at the top level. I could make a PR for that when I get a chance, probably today or tomorrow.

@jgerigmeyer
Copy link
Member

Ah, I found one that works:

import {ColorTypes} from 'colorjs.io/types/src/color';

At least there's a sane way to access them. Still, this seems like an accidental feature; I'd expect to be able to access the types directly from a colorjs.io import.

Yeah, I agree. I've been accessing them this same way (e.g. import type { PlainColorObject } from 'colorjs.io/types/src/color';), but it makes sense to export them at the top level. @MysteryBlokHed I'm happy to review your PR once it's submitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants