Skip to content

Commit

Permalink
ts: Add missing IDL types
Browse files Browse the repository at this point in the history
The following new IDL types were introduced in coral-xyz#2011:

* `GenericLenArray`
* `Generic`
* `DefinedWithTypeArgs`

Usage of these types was leading to incompatibility of the IDL with
the TypeScript `IdlType`.

Fixes: coral-xyz#2687
  • Loading branch information
vadorovsky committed Oct 28, 2023
1 parent 5a655b0 commit efc7155
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/idl/tests/generics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as anchor from "@coral-xyz/anchor";
import { Idl } from "@coral-xyz/anchor";
import { expect } from "chai";

import { Generics, IDL } from "../target/types/generics";

describe("Generics", () => {
anchor.setProvider(anchor.AnchorProvider.env());

/**
* Checks if the IDL produced by Anchor CLI is compatible with the TypeScript
* `Idl` type. Detects a potential mismatch between Rust and TypeScript IDL
* definitions.
*/
it("Raw IDL", () => {
const idl: Idl = IDL;
expect(idl).to.not.be.undefined;
});
});
34 changes: 33 additions & 1 deletion ts/packages/anchor/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export type IdlType =
| IdlTypeOption
| IdlTypeCOption
| IdlTypeVec
| IdlTypeArray;
| IdlTypeArray
| IdlTypeGenericLenArray
| IdlTypeGeneric
| IdlTypeDefinedWithTypeArgs;

// User defined type.
export type IdlTypeDefined = {
Expand All @@ -162,6 +165,35 @@ export type IdlTypeArray = {
array: [idlType: IdlType, size: number];
};

export type IdlTypeGenericLenArray = {
genericLenArray: [idlType: IdlType, generic: string];
};

export type IdlTypeGeneric = {
generic: string;
};

export type IdlTypeDefinedWithTypeArgs = {
definedWithTypeArgs: { name: string; args: IdlTypeDefinedTypeArg[] };
};

export type IdlTypeDefinedTypeArg =
| IdlTypeDefinedTypeArgGeneric
| IdlTypeDefinedTypeArgValue
| IdlTypeDefinedTypeArgType;

export type IdlTypeDefinedTypeArgGeneric = {
generic: string;
};

export type IdlTypeDefinedTypeArgValue = {
value: string;
};

export type IdlTypeDefinedTypeArgType = {
type: IdlType;
};

export type IdlEnumVariant = {
name: string;
fields?: IdlEnumFields;
Expand Down

0 comments on commit efc7155

Please sign in to comment.