Closed Gozala closed 4 years ago
For example:
/**
* @param {string|Uint8Array|CID} cid
* ...
*/
function (cid) {
cid = new CID(cid)
//...
}
No overload matches this call.
Overload 1 of 4, '(cid: CID): CID', gave the following error.
Argument of type 'string | CID | Uint8Array' is not assignable to parameter of type 'CID'.
Type 'string' is not assignable to type 'CID'.
Overload 2 of 4, '(str: string): CID', gave the following error.
Argument of type 'string | CID | Uint8Array' is not assignable to parameter of type 'string'.
Type 'CID' is not assignable to type 'string'.
Overload 3 of 4, '(buf: Uint8Array): CID', gave the following error.
Argument of type 'string | CID | Uint8Array' is not assignable to parameter of type 'Uint8Array'.
Type 'string' is not assignable to type
Ok here is the solution to all these problems:
Inline below
/**
* Class representing a CID `<mbase><version><mcodec><mhash>`
* , as defined in [ipld/cid](https://github.com/multiformats/cid).
*/
declare class CID {
/**
* Create a new CID.
*
* The algorithm for argument input is roughly:
* ```
* if (cid)
* -> create a copy
* else if (str)
* if (1st char is on multibase table) -> CID String
* else -> bs58 encoded multihash
* else if (Uint8Array)
* if (1st byte is 0 or 1) -> CID
* else -> multihash
* else if (Number)
* -> construct CID by parts
* ```
*
* @example
* new CID(<version>, <codec>, <multihash>, <multibaseName>)
* new CID(<cidStr>)
* new CID(<cid.bytes>)
* new CID(<multihash>)
* new CID(<bs58 encoded multihash>)
* new CID(<cid>)
*/
constructor(
version: CIDVersion,
codec: string | number,
multhash: Uint8Array,
multibaseName?: string
);
constructor(cid: CID|string|Uint8Array);
/**
* The version of the CID.
*/
readonly version: CIDVersion;
/**
* The codec of the CID.
*/
readonly codec: string;
/**
* The codec of the CID in its number form.
*/
readonly code: number;
/**
* The multihash of the CID.
*/
readonly multihash: Uint8Array;
/**
* Multibase name as string.
*/
readonly multibaseName: string;
/**
* The CID as a `Uint8Array`
*/
readonly bytes: Uint8Array;
/**
* The prefix of the CID.
*/
readonly prefix: Uint8Array;
/**
* Convert to a CID of version `0`.
*/
toV0(): CID;
/**
* Convert to a CID of version `1`.
*/
toV1(): CID;
/**
* Encode the CID into a string.
*
* @param base Base encoding to use.
*/
toBaseEncodedString(base?: string): string;
/**
* Encode the CID into a string.
*/
toString(base?: string): string;
/**
* Serialize to a plain object.
*/
toJSON(): { codec: string; version: 0 | 1; hash: Uint8Array };
/**
* Compare equality with another CID.
*
* @param other The other CID.
*/
equals(other: any): boolean;
/**
* Test if the given input is a valid CID object.
* Throws if it is not.
*
* @param other The other CID.
*/
static validateCID(other: any): void;
static isCID(mixed: any): mixed is CID;
static codecs: Record<string, number>;
}
type CIDVersion = 0|1
const from = (cid:string|Uint8Array|CID) => new CID(cid)
const parse = (cid:string) => new CID(cid)
const clone = (cid:CID) => new CID(cid)
const copy = (cid:CID) => new CID(cid.version, cid.codec, cid.multihash)
const decode = (cid:Uint8Array) => new CID(cid)
const create = (version:number, codec: number, multihash: Uint8Array) => {
if (version === 0 || version === 1) {
return new CID(version, codec, multihash)
} else {
throw Error('Invalid version')
}
}
🙏 Please PR 😄
OK, so the fix here addresses assigning to a CID|string|Uint8Array
from a function that only returns either CID
, string
, or Uint8Array
. 🙃. It's really a TypeScript bug that we have to address and not technically a bug in our definitions, right?
Current type defs for the CIDs have bunch of overloads for the construction function, which seems to confuse typescript in some call patterns.