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

Component Keys #4

Open
Cynosphere opened this issue Oct 2, 2024 · 1 comment
Open

Component Keys #4

Cynosphere opened this issue Oct 2, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Cynosphere
Copy link
Member

Cannot import destructure from Components

@Cynosphere Cynosphere added this to API v2 Oct 2, 2024
@NotNite NotNite moved this to Todo in API v2 Oct 2, 2024
@NotNite NotNite closed this as completed Oct 2, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in API v2 Oct 2, 2024
@Cynosphere Cynosphere reopened this Oct 8, 2024
@Cynosphere Cynosphere added this to 1.2.0 Oct 8, 2024
@Cynosphere Cynosphere moved this to Todo in 1.2.0 Oct 8, 2024
@NotNite
Copy link
Member

NotNite commented Oct 8, 2024

I'm not sure this is even possible to solve without just typing everything (which we could but seems jank). This might be a TypeScript bug? Anyways, let me explain the problem for anyone else who was confused by the issue name.


The components type does this:

type CommonComponents = {
  [index: string]: any;
  // other typed components follow
};

This means that you can use typed components such as Text, but ones we don't have typed like Notice still work (they're just any). Without that index signature, you couldn't import Notice here without an error:

// Property 'Notice' does not exist on type 'CommonComponents'.ts(2339)

const { Text, Notice } = require("discord/components/common/index");
// or
const Components = require("discord/components/common/index");
const Notice = Components.Notice;
// or
import * as Components from "@moonlight-mod/wp/discord/components/common/index";
const Notice = Components.Notice;

...but with that index signature, Text will be the typed component, and Notice will be any in all of these examples.

The mappings repo generates the following declare (this is actually wrong for some types with default but it's valid here):

declare module "@moonlight-mod/wp/discord/components/common/index" {
  import { MappedModules } from "@moonlight-mod/mappings";
  const _: MappedModules["discord/components/common/index"];
  export = _;
}

(The _ is just the components type, by the way.)

You would expect the TypeScript compiler to be able to recognize that index signature, but it can't:

// Module '"@moonlight-mod/wp/discord/components/common/index"' has no exported member 'Notice'.ts(2305)

import { Text, Notice } from "@moonlight-mod/wp/discord/components/common/index";

This works, but isn't desirable because it looks funky:

import * as Components from "@moonlight-mod/wp/discord/components/common/index";
const { Notice } = Components;
// or
const Notice = Components.Notice;

Looks like, when resolving imports to destructure, TypeScript doesn't consider the index signature. I'll try and get a reproduction case going and make an issue.

@NotNite NotNite removed this from 1.2.0 Oct 9, 2024
@NotNite NotNite added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants