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

Type generation creates UseridMap as an interface when it is a Map/Record/object #1584

Open
1 of 7 tasks
rockingskier opened this issue Jan 24, 2023 · 3 comments
Open
1 of 7 tasks
Labels
area:typescript issues that specifically impact using the package from typescript projects enhancement M-T: A feature request for new functionality good first issue pkg:web-api applies to `@slack/web-api`
Milestone

Comments

@rockingskier
Copy link
Contributor

rockingskier commented Jan 24, 2023

When generating types for packages/web-api/src/response/MigrationExchangeResponse.ts, UseridMap is created as an interface when really it should be a Record.

This means when calling migration.exchange the data in response.user_id_map needs to be cast before it can used.

// Warning: pseudo code alert
type UserIds = Partial<Record<string, string>>;

const migrateUserIds = (userIds: string[]): UserIds => {
  const response = await client.migration.exchange({
    team_id: "WORKSPACEID",
    users: userIds.join(","),
  });
  
  if (!response.ok) return {};
  return return response.user_id_map as UserIds;  // Mmmmm
}

Packages:

Select all that apply:

  • @slack/web-api
  • @slack/rtm-api
  • @slack/webhooks
  • @slack/oauth
  • @slack/socket-mode
  • @slack/types
  • I don't know

Reproducible in:

Whatever is in main at the moment: 57dfdda

Steps to reproduce:

./generate-web-api-types.sh

Expected result:

# packages/web-api/src/response/MigrationExchangeResponse.ts
// ...
export type UseridMap  = Record<string, string>  // Should probably be a `Partial` or `string | undefined`

Actual result:

# packages/web-api/src/response/MigrationExchangeResponse.ts
// ...
export interface UseridMap {
}
@seratch seratch added good first issue enhancement M-T: A feature request for new functionality area:typescript issues that specifically impact using the package from typescript projects pkg:web-api applies to `@slack/web-api` and removed untriaged labels Jan 24, 2023
@seratch seratch added this to the web-api@6.x milestone Jan 24, 2023
@seratch
Copy link
Member

seratch commented Jan 24, 2023

Hi @rockingskier, thanks for taking the time to report this! I agree that this should be improved in future versions

@rockingskier
Copy link
Contributor Author

I've been having a dig around...

This is the Java class: https://github.com/slackapi/java-slack-sdk/blob/main/slack-api-client/src/main/java/com/slack/api/methods/response/migration/MigrationExchangeResponse.java#L37
This is the example json: https://github.com/slackapi/java-slack-sdk/blob/main/json-logs/samples/api/migration.exchange.json#L12

The generator uses quicktype to convert the JSON to TS. It uses "the dark arts" (Markov chain - https://blog.quicktype.io/markov/) to decide when an object should be an interface or a map.

If quicktype thinks your object is a class when it should be a map, a bit more work is needed. The easiest way to correct this is to trick quicktype by making the property names look "weird":

I dropped the example JSON into https://app.quicktype.io/ and had a quick go and tricking quicktype but had no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typescript issues that specifically impact using the package from typescript projects enhancement M-T: A feature request for new functionality good first issue pkg:web-api applies to `@slack/web-api`
Projects
None yet
Development

No branches or pull requests

2 participants