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

Add Caip Types #116

Merged
merged 14 commits into from
Aug 1, 2023
138 changes: 138 additions & 0 deletions src/caip-types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { expectAssignable, expectNotAssignable } from 'tsd';

import {

Check failure on line 3 in src/caip-types.test-d.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (16.x)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 3 in src/caip-types.test-d.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 3 in src/caip-types.test-d.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

All imports in the declaration are only used as types. Use `import type`
CaipAccountId,
CaipChain,
CaipChainId,
CaipNamespace,
CaipNamespaceId,
} from '.';

const embeddedString = 'test';

// Valid caip strings:

expectAssignable<CaipChainId>('namespace:reference');
expectAssignable<CaipChainId>('namespace:');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that this is non-enforceable via TypeScript, is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not. We could try to hack it by doing something like this

type Character = "a" | "b" | "c" | "d" | "e" | "f" | "g" | ...

export type CaipChainId = `${Character}${string}:${Character}${string}`;

But I believe this makes a type with n^2 definitions since they get crossmulitplied

expectAssignable<CaipChainId>(':reference');
expectAssignable<CaipChainId>(`${embeddedString}:${embeddedString}`);

expectAssignable<CaipAccountId>('namespace:reference:accountAddress');
expectAssignable<CaipAccountId>('namespace:reference:');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar as above — I'm assuming this is non-enforceable via TypeScript?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

expectAssignable<CaipAccountId>(':reference:accountAddress');
expectAssignable<CaipAccountId>(
`${embeddedString}:${embeddedString}:${embeddedString}`,
);

expectAssignable<CaipNamespaceId>('string');
expectAssignable<CaipNamespaceId>(`${embeddedString}`);

// Not valid caip strings:

expectAssignable<CaipChainId>('namespace:😀');
expectAssignable<CaipChainId>('😀:reference');
expectNotAssignable<CaipChainId>(0);
expectNotAssignable<CaipChainId>('🙃');

expectAssignable<CaipAccountId>('namespace:reference:😀');
expectAssignable<CaipAccountId>('😀:reference:accountAddress');
expectNotAssignable<CaipAccountId>(0);
expectNotAssignable<CaipAccountId>('🙃');

expectNotAssignable<CaipNamespaceId>(0);

// Valid caip objects:

expectAssignable<CaipChain>({
id: 'string',
name: 'string',
});
expectAssignable<CaipChain>({
id: `${embeddedString}`,
name: `${embeddedString}`,
});

expectAssignable<CaipNamespace>({
chains: [
{
id: 'string',
name: 'string',
},
{
id: `${embeddedString}`,
name: `${embeddedString}`,
},
],
});
expectAssignable<CaipNamespace>({
chains: [
{
id: 'string',
name: 'string',
},
{
id: `${embeddedString}`,
name: `${embeddedString}`,
},
],
methods: ['string', `${embeddedString}`],
events: ['string', `${embeddedString}`],
});

// Not valid caip objects:
expectNotAssignable<CaipChain>('');
expectNotAssignable<CaipChain>(0);
expectNotAssignable<CaipChain>({});
expectNotAssignable<CaipChain>({
id: 'string',
});
expectNotAssignable<CaipChain>({
name: 'string',
});
expectNotAssignable<CaipChain>({
id: 0,
name: 'string',
});
expectNotAssignable<CaipChain>({
id: 'string',
name: 0,
});

expectNotAssignable<CaipNamespace>('');
expectNotAssignable<CaipNamespace>(0);
expectNotAssignable<CaipNamespace>({});
expectNotAssignable<CaipNamespace>({
chains: [
'',
0,
{},
{
id: 'string',
name: 0,
},
{
id: 'string',
},
{
name: 'string',
},
],
});
expectNotAssignable<CaipNamespace>({
chains: [
{
id: 'string',
name: 'string',
},
],
methods: [0],
});
expectNotAssignable<CaipNamespace>({
chains: [
{
id: 'string',
name: 'string',
},
],
events: [0],
});
Loading
Loading