Tighten up TS type with LookupString introduction (#5446)
* Tighten up TS type with LookupString introduction * Adjust typegen
This commit is contained in:
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user