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

refactor(experimental): reduce getProgramAccounts types using generics #1531

Closed
wants to merge 1 commit into from

Conversation

steveluscher
Copy link
Collaborator

@steveluscher steveluscher commented Aug 22, 2023

refactor(experimental): reduce getProgramAccounts types using generics

Summary

This so close to works. You can see the types in action here:

const address = 'abc' as Base58EncodedAddress;

/** No encoding */
const noEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
withContext: true,
});
const noEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    withContext: false,
});
const noEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address);

/** Base58 */
const base58EncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
    withContext: true,
});
const base58EncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
    withContext: false,
});
const base58EncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
});

/** Base64 */
const base64EncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
    withContext: true,
});
const base64EncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
    withContext: false,
});
const base64EncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
});

/** Base64-zstd */
const base64ZStdEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
    withContext: true,
});
const base64ZStdEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
    withContext: false,
});
const base64ZStdEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
});

/** JSON parsed */
const jsonParsedEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
    withContext: true,
});
const jsonParsedEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
    withContext: false,
});
const jsonParsedEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
});

There's one TypeScript bug, though, that makes rpc.getProgramAccounts() ultimately lose all of its type information. It's because of this: microsoft/TypeScript#55435 (comment)

@steveluscher steveluscher marked this pull request as draft August 22, 2023 20:25
# Summary

This _so_ close to works. You can see the types in action here:

```
const address = 'abc' as Base58EncodedAddress;

/** No encoding */
const noEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
withContext: true,
});
const noEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    withContext: false,
});
const noEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address);

/** Base58 */
const base58EncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
    withContext: true,
});
const base58EncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
    withContext: false,
});
const base58EncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base58',
});

/** Base64 */
const base64EncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
    withContext: true,
});
const base64EncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
    withContext: false,
});
const base64EncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64',
});

/** Base64-zstd */
const base64ZStdEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
    withContext: true,
});
const base64ZStdEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
    withContext: false,
});
const base64ZStdEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'base64+zstd',
});

/** JSON parsed */
const jsonParsedEncodingWithContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
    withContext: true,
});
const jsonParsedEncodingFalseContext = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
    withContext: false,
});
const jsonParsedEncodingNoContextConfig = (null as unknown as GetProgramAccountsApi).getProgramAccounts(address, {
    encoding: 'jsonParsed',
});
```

There's _one_ TypeScript bug, though, that makes `rpc.getProgramAccounts()` ultimately lose all of its type information. It's because of this: microsoft/TypeScript#55435 (comment)
@mcintyre94
Copy link
Contributor

This is awesome! Do you have any idea how tricky the fix would be on the Typescript side?

@steveluscher
Copy link
Collaborator Author

I'm not sure. I feel like it's a straightforward bug with infer, but nothing ever is.

It infers just fine if you:

  1. Supply <any> for the type parameter
  2. Don't have the return type depend on the type parameter

I feel like it should absolutely infer the full range of possible types when the type parameter is not supplied.

@github-actions
Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2023
@steveluscher steveluscher deleted the pr1531 branch December 2, 2023 09:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants