Enhance type narrow checks (#3939)

This commit is contained in:
Jaco
2021-09-08 08:07:07 +02:00
committed by GitHub
parent d1c9a507d9
commit fca973cd53
2 changed files with 12 additions and 6 deletions
+5 -3
View File
@@ -208,12 +208,14 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
/** @internal */
// eslint-disable-next-line @typescript-eslint/ban-types
private static _decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
if (value instanceof GenericExtrinsicEra) {
if (!value) {
return new Uint8Array([0]);
} else if (value instanceof GenericExtrinsicEra) {
return GenericExtrinsicEra._decodeExtrinsicEra(value.toU8a());
} else if (isHex(value)) {
return GenericExtrinsicEra._decodeExtrinsicEra(hexToU8a(value));
} else if (!value || isU8a(value)) {
return (!value?.length || value[0] === 0)
} else if (isU8a(value)) {
return (!value.length || value[0] === 0)
? new Uint8Array([0])
: new Uint8Array([1, value[0], value[1]]);
} else if (isObject(value)) {
+7 -3
View File
@@ -15,11 +15,15 @@ const EMPTY_METADATA = new Uint8Array([0x6d, 0x65, 0x74, 0x61, 9]);
const EMPTY_U8A = new Uint8Array();
function toU8a (value: Uint8Array | string = EMPTY_U8A): Uint8Array {
return isHex(value)
? toU8a(u8aToU8a(value))
: isU8a(value) && value.length === 0
if (isHex(value)) {
return toU8a(u8aToU8a(value));
} else if (isU8a(value)) {
return value.length === 0
? EMPTY_METADATA
: value;
}
throw new Error('Invalid type passed to Metadata constructor');
}
function decodeMetadata (registry: Registry, _value?: Uint8Array | string | Map<string, unknown> | Record<string, unknown>): MetadataVersioned {