Tighten up TS type with LookupString introduction (#5446)
* Tighten up TS type with LookupString introduction * Adjust typegen
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
import type { AnyString, Registry } from '@polkadot/types/types';
|
||||
import type { LookupString } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '@polkadot/types-create/types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
@@ -189,7 +190,7 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
|
||||
},
|
||||
|
||||
[TypeDefInfo.Si]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
||||
return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type), imports, withShortcut);
|
||||
return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type as LookupString), imports, withShortcut);
|
||||
},
|
||||
|
||||
[TypeDefInfo.Struct]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
||||
|
||||
@@ -33,3 +33,5 @@ export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
|
||||
export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 | 1024 | 2048;
|
||||
|
||||
export type AnyTupleValue = Exclude<AnyU8a, string> | HexString | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
|
||||
|
||||
export type LookupString = `Lookup${number}`;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { Codec, CodecClass } from './codec';
|
||||
import type { AnyTuple } from './helpers';
|
||||
import type { AnyTuple, LookupString } from './helpers';
|
||||
import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces';
|
||||
|
||||
export type OnlyCodec<K, T, Types> =
|
||||
@@ -59,24 +59,14 @@ export interface Registry {
|
||||
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
|
||||
findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
|
||||
|
||||
isLookupType (value: string): boolean;
|
||||
createLookupType (lookupId: ICompact<INumber> | number): string;
|
||||
|
||||
// createClass <T, K extends string = string, Types = Record<K, Codec>> (type: K): CodecClass<OnlyCodec<T, K, Types>>;
|
||||
isLookupType (value: string): value is LookupString;
|
||||
createLookupType (lookupId: ICompact<INumber> | number): LookupString;
|
||||
createClassUnsafe <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<T>;
|
||||
// createType <T, K extends string = string> (type: K, ...params: unknown[]): OnlyCodec<T, K, Types>;
|
||||
createTypeUnsafe <T extends Codec = Codec, K extends string = string> (type: K, params: unknown[], options?: CodecCreateOptions): T;
|
||||
|
||||
// get <T extends Codec = Codec, K extends string = string, R = T> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<R> | undefined;
|
||||
// getChainProperties (): ChainProperties | undefined;
|
||||
getClassName (clazz: CodecClass): string | undefined;
|
||||
// getDefinition (typeName: string): string | undefined;
|
||||
// getModuleInstances (specName: string, moduleName: string): string[] | undefined;
|
||||
|
||||
getOrThrow <T extends Codec = Codec, K extends string = string> (name: K, msg?: string): CodecClass<T>;
|
||||
getOrUnknown <T extends Codec = Codec, K extends string = string> (name: K): CodecClass<T>;
|
||||
|
||||
// setKnownTypes (types: RegisteredTypes): void;
|
||||
getSignedExtensionExtra (): Record<string, string>;
|
||||
getSignedExtensionTypes (): Record<string, string>;
|
||||
|
||||
@@ -84,13 +74,8 @@ export interface Registry {
|
||||
hasDef (name: string): boolean;
|
||||
hasType (name: string): boolean;
|
||||
hash (data: Uint8Array): IU8a;
|
||||
// init (): Registry;
|
||||
register (type: CodecClass | RegistryTypes): void;
|
||||
register (name: string, type: CodecClass): void;
|
||||
register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void;
|
||||
// setChainProperties (properties?: ChainProperties): void;
|
||||
// setHasher (hasher?: IU8aer | null): void;
|
||||
// setLookup (lookup: PortableRegistry): void;
|
||||
setMetadata (metadata: unknown, signedExtensions?: string[], userExtensions?: unknown): void;
|
||||
// setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2023 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, CodecClass, Registry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';
|
||||
import type { Codec, CodecClass, LookupString, Registry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '../types';
|
||||
|
||||
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from '@polkadot/types-codec';
|
||||
@@ -161,7 +161,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
|
||||
),
|
||||
|
||||
[TypeDefInfo.Si]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
getTypeClass(registry, registry.lookup.getTypeDef(value.type)),
|
||||
getTypeClass(registry, registry.lookup.getTypeDef(value.type as LookupString)),
|
||||
|
||||
[TypeDefInfo.Struct]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
Struct.with(getTypeClassMap(value), value.alias),
|
||||
|
||||
@@ -5,16 +5,15 @@
|
||||
// this is required to allow for ambient/previous definitions
|
||||
import '@polkadot/types-codec/types/registry';
|
||||
|
||||
import type { Codec, CodecClass, ICompact, INumber } from '@polkadot/types-codec/types';
|
||||
import type { Codec, CodecClass, ICompact, INumber, LookupString } from '@polkadot/types-codec/types';
|
||||
import type { ILookup, TypeDef } from '@polkadot/types-create/types';
|
||||
|
||||
declare module '@polkadot/types-codec/types/registry' {
|
||||
interface Registry {
|
||||
readonly lookup: ILookup;
|
||||
|
||||
createLookupType (lookupId: ICompact<INumber> | number): string;
|
||||
setLookup (lookup: ILookup): void;
|
||||
|
||||
createLookupType (lookupId: ICompact<INumber> | number): LookupString;
|
||||
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
|
||||
setLookup (lookup: ILookup): void;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,20 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Option, Text } from '@polkadot/types-codec';
|
||||
import type { ICompact, INumber } from '@polkadot/types-codec/types';
|
||||
import type { ICompact, INumber, LookupString } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from './types';
|
||||
|
||||
// A simplified SiType without the need for an interface import
|
||||
// (while type interfaces are still in @polkadot/types). This provides
|
||||
// the minimum interface allowing us to work with it here
|
||||
interface SiTypeBase {
|
||||
def: {
|
||||
asTuple: ICompact<INumber>[]
|
||||
}
|
||||
}
|
||||
|
||||
export interface ILookup {
|
||||
getSiType (lookupId: ICompact<INumber> | string | number): {
|
||||
def: {
|
||||
asTuple: ICompact<INumber>[]
|
||||
}
|
||||
},
|
||||
getTypeDef (lookupId: ICompact<INumber> | string | number): TypeDef;
|
||||
getSiType (lookupId: ICompact<INumber> | LookupString | number): SiTypeBase;
|
||||
getTypeDef (lookupId: ICompact<INumber> | LookupString | number): TypeDef;
|
||||
sanitizeField (name: Option<Text>): [string | null, string | null];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2023 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyString, Codec, CodecClass, IU8a } from '@polkadot/types-codec/types';
|
||||
import type { AnyString, Codec, CodecClass, IU8a, LookupString } from '@polkadot/types-codec/types';
|
||||
import type { CreateOptions, TypeDef } from '@polkadot/types-create/types';
|
||||
import type { ExtDef } from '../extrinsic/signedExtensions/types';
|
||||
import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant, WeightV2 } from '../interfaces/types';
|
||||
@@ -240,15 +240,15 @@ export class TypeRegistry implements Registry {
|
||||
/**
|
||||
* @description Returns true if the type is in a Compat format
|
||||
*/
|
||||
public isLookupType (value: string): boolean {
|
||||
public isLookupType (value: string): value is LookupString {
|
||||
return /Lookup\d+$/.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates a lookup string from the supplied id
|
||||
*/
|
||||
public createLookupType (lookupId: SiLookupTypeId | number): string {
|
||||
return `Lookup${lookupId.toString()}`;
|
||||
public createLookupType (lookupId: SiLookupTypeId | number): LookupString {
|
||||
return `Lookup${typeof lookupId === 'number' ? lookupId : lookupId.toNumber()}`;
|
||||
}
|
||||
|
||||
public get knownTypes (): RegisteredTypes {
|
||||
@@ -256,7 +256,7 @@ export class TypeRegistry implements Registry {
|
||||
}
|
||||
|
||||
public get lookup (): PortableRegistry {
|
||||
return assertReturn(this.#lookup, 'Lookup has not been set on this registry');
|
||||
return assertReturn(this.#lookup, 'PortableRegistry has not been set on this registry');
|
||||
}
|
||||
|
||||
public get metadata (): MetadataLatest {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Option, Text, Type, Vec } from '@polkadot/types-codec';
|
||||
import type { AnyString, Registry } from '@polkadot/types-codec/types';
|
||||
import type { AnyString, LookupString, Registry } from '@polkadot/types-codec/types';
|
||||
import type { ILookup, TypeDef } from '@polkadot/types-create/types';
|
||||
import type { PortableType } from '../../interfaces/metadata';
|
||||
import type { SiField, SiLookupTypeId, SiType, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '../../interfaces/scaleInfo';
|
||||
@@ -25,7 +25,7 @@ interface Extract extends ExtractBase {
|
||||
}
|
||||
|
||||
interface TypeInfo {
|
||||
lookups: Record<string, string>;
|
||||
lookups: Record<string, LookupString>;
|
||||
names: Record<number, string>;
|
||||
params: Record<string, SiTypeParameter[]>;
|
||||
types: Record<number, PortableType>;
|
||||
@@ -470,7 +470,7 @@ function extractTypeInfo (lookup: PortableRegistry, portable: PortableType[]): T
|
||||
types[lookupIndex] = type;
|
||||
}
|
||||
|
||||
const lookups: Record<string, string> = {};
|
||||
const lookups: Record<string, LookupString> = {};
|
||||
const names: Record<number, string> = {};
|
||||
const params: Record<string, SiTypeParameter[]> = {};
|
||||
const dedup = removeDupeNames(lookup, portable, nameInfo);
|
||||
@@ -489,7 +489,7 @@ function extractTypeInfo (lookup: PortableRegistry, portable: PortableType[]): T
|
||||
|
||||
export class PortableRegistry extends Struct implements ILookup {
|
||||
#alias: Record<number, string>;
|
||||
#lookups: Record<string, string>;
|
||||
#lookups: Record<string, LookupString>;
|
||||
#names: Record<number, string>;
|
||||
#params: Record<string, SiTypeParameter[]>;
|
||||
#typeDefs: Record<number, TypeDef> = {};
|
||||
@@ -537,14 +537,14 @@ export class PortableRegistry extends Struct implements ILookup {
|
||||
/**
|
||||
* @description Returns the name for a specific lookup
|
||||
*/
|
||||
public getName (lookupId: SiLookupTypeId | string | number): string | undefined {
|
||||
public getName (lookupId: SiLookupTypeId | LookupString | number): string | undefined {
|
||||
return this.#names[this.#getLookupId(lookupId)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Finds a specific type in the registry
|
||||
*/
|
||||
public getSiType (lookupId: SiLookupTypeId | string | number): SiType {
|
||||
public getSiType (lookupId: SiLookupTypeId | LookupString | number): SiType {
|
||||
// NOTE catch-22 - this may already be used as part of the constructor, so
|
||||
// ensure that we have actually initialized it correctly
|
||||
const found = (this.#types || this.types)[this.#getLookupId(lookupId)];
|
||||
@@ -559,7 +559,7 @@ export class PortableRegistry extends Struct implements ILookup {
|
||||
/**
|
||||
* @description Lookup the type definition for the index
|
||||
*/
|
||||
public getTypeDef (lookupId: SiLookupTypeId | string | number): TypeDef {
|
||||
public getTypeDef (lookupId: SiLookupTypeId | LookupString | number): TypeDef {
|
||||
const lookupIndex = this.#getLookupId(lookupId);
|
||||
|
||||
if (!this.#typeDefs[lookupIndex]) {
|
||||
@@ -640,10 +640,10 @@ export class PortableRegistry extends Struct implements ILookup {
|
||||
}
|
||||
|
||||
/** @internal Converts a lookupId input to the actual lookup index */
|
||||
#getLookupId (lookupId: SiLookupTypeId | string | number): number {
|
||||
#getLookupId (lookupId: SiLookupTypeId | LookupString | number): number {
|
||||
if (isString(lookupId)) {
|
||||
if (!this.registry.isLookupType(lookupId)) {
|
||||
throw new Error(`PortableRegistry: Expected a lookup string type, found ${lookupId}`);
|
||||
throw new Error(`PortableRegistry: Expected a lookup string type, found ${lookupId as string}`);
|
||||
}
|
||||
|
||||
return parseInt(lookupId.replace('Lookup', ''), 10);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
import '@polkadot/types-create/types/lookup';
|
||||
|
||||
import type { Vec } from '@polkadot/types-codec';
|
||||
import type { LookupString } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '@polkadot/types-create/types';
|
||||
import type { PortableType } from '../interfaces/metadata';
|
||||
import type { SiLookupTypeId, SiType } from '../interfaces/scaleInfo';
|
||||
@@ -15,8 +16,8 @@ declare module '@polkadot/types-create/types/lookup' {
|
||||
readonly names: string[];
|
||||
readonly types: Vec<PortableType>;
|
||||
|
||||
getName (lookupId: SiLookupTypeId | string | number): string | undefined;
|
||||
getSiType (lookupId: SiLookupTypeId | string | number): SiType;
|
||||
getTypeDef (lookupId: SiLookupTypeId | string | number): TypeDef;
|
||||
getName (lookupId: SiLookupTypeId | LookupString | number): string | undefined;
|
||||
getSiType (lookupId: SiLookupTypeId | LookupString | number): SiType;
|
||||
getTypeDef (lookupId: SiLookupTypeId | LookupString | number): TypeDef;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import '@polkadot/types-codec/types/registry';
|
||||
import '@polkadot/types-create/types/augmentRegistry';
|
||||
|
||||
import type { AnyString, Codec, CodecClass } from '@polkadot/types-codec/types';
|
||||
import type { AnyString, Codec, CodecClass, LookupString } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '@polkadot/types-create/types';
|
||||
import type { ExtDef } from '../extrinsic/signedExtensions/types';
|
||||
import type { MetadataLatest } from '../interfaces/metadata';
|
||||
@@ -33,7 +33,7 @@ declare module '@polkadot/types-codec/types/registry' {
|
||||
|
||||
clearCache (): void
|
||||
|
||||
createLookupType (lookupId: SiLookupTypeId | number): string;
|
||||
createLookupType (lookupId: SiLookupTypeId | number): LookupString;
|
||||
|
||||
createClass <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<DetectCodec<T, K>>;
|
||||
createType <T extends Codec = Codec, K extends string = string> (type: K, ...params: unknown[]): DetectCodec<T, K>;
|
||||
|
||||
Reference in New Issue
Block a user