From 68cfd33a6bb8392060f340a9071abaed29fa1e59 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Sun, 29 Oct 2023 00:48:28 +0200 Subject: [PATCH] ts: Add missing IDL types The following new IDL types were introduced in #2011: * `GenericLenArray` * `Generic` * `DefinedWithTypeArgs` Usage of these types was leading to incompatibility of the IDL with the TypeScript `IdlType`. Fixes: #2687 --- tests/idl/tests/generics.ts | 19 +++++++++++++++++++ ts/packages/anchor/src/idl.ts | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/idl/tests/generics.ts diff --git a/tests/idl/tests/generics.ts b/tests/idl/tests/generics.ts new file mode 100644 index 0000000000..40b6de49be --- /dev/null +++ b/tests/idl/tests/generics.ts @@ -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; + }); +}); diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index dc88cd586e..61242b0cac 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -139,7 +139,10 @@ export type IdlType = | IdlTypeOption | IdlTypeCOption | IdlTypeVec - | IdlTypeArray; + | IdlTypeArray + | IdlTypeGenericLenArray + | IdlTypeGeneric + | IdlTypeDefinedWithTypeArgs; // User defined type. export type IdlTypeDefined = { @@ -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;