compound-js icon indicating copy to clipboard operation
compound-js copied to clipboard

cToken constants missing from Compound typescript definitions

Open 0xbhagwan opened this issue 5 years ago • 2 comments

Hello,

Importing the library into a typescript project and accessing any of the properties defined in src/constants.ts such as Compound.cDAI as in the example in the readme throws the following error:

[tsserver 2339] [E] Property 'cDai' does not exist on type '{ (provider?:       
                           string | Provider, options?: CompoundOptions): CompoundInstance; eth: typeof

Reproducing

On a typescript file with strict mode enabled:

import Compound from "@compound-finance/compound-js";

console.log(Compound.cDai);

Cause

The generated type definition file at 'dist/nodejs/index.d.ts' the constants such as cDAI are missing:

...
declare const Compound: {
    (provider?: Provider | string, options?: CompoundOptions): CompoundInstance;
    eth: typeof eth;
    api: typeof api;
    util: typeof util;
    _ethers: typeof ethers;
    decimals: {
        cBAT: number;
        cCOMP: number;
        cDAI: number;
        cETH: number;
        cREP: number;
        cSAI: number;
        cUNI: number;
        cUSDC: number;
        cUSDT: number;
        cWBTC: number;
        cZRX: number;
        BAT: number;
        COMP: number;
        DAI: number;
        ETH: number;
        REP: number;
        SAI: number;
        UNI: number;
        USDC: number;
        USDT: number;
        WBTC: number;
        ZRX: number;
        KNC: number;
        LINK: number;
        BTC: number;
    };
    comp: {
        getCompBalance: typeof comp.getCompBalance;
        getCompAccrued: typeof comp.getCompAccrued;
    };
};
...

This is because on src/index.ts:93 the constant properties are assigned with Object.assign(Compound, constants); which does not preserve the types.

Proposed fix

Stop using object assign and use the spread operator instead, since types are preserved that way.

Then 'dist/nodejs/index.d.ts' becomes:

...
declare const Compound: {
    PriceFeed: string;
    Maximillion: string;
    CompoundLens: string;
    GovernorAlpha: string;
    Comptroller: string;
    Reservoir: string;
    KNC: string;
    LINK: string;
    BTC: string;
    cBAT: string;
    cCOMP: string;
    cDAI: string;
    cETH: string;
    cREP: string;
    cSAI: string;
    cUNI: string;
    cUSDC: string;
    cUSDT: string;
    cWBTC: string;
    cZRX: string;
    BAT: string;
    COMP: string;
    DAI: string;
    ETH: string;
    REP: string;
    SAI: string;
    UNI: string;
    USDC: string;
    USDT: string;
    WBTC: string;
    ZRX: string;
    eth: typeof eth;
    api: typeof api;
    util: typeof util;
    _ethers: typeof ethers;
    decimals: {
        cBAT: number;
        cCOMP: number;
        cDAI: number;
        cETH: number;
        cREP: number;
        cSAI: number;
        cUNI: number;
        cUSDC: number;
        cUSDT: number;
        cWBTC: number;
        cZRX: number;
        BAT: number;
        COMP: number;
        DAI: number;
        ETH: number;
        REP: number;
        SAI: number;
        UNI: number;
        USDC: number;
        USDT: number;
        WBTC: number;
        ZRX: number;
        KNC: number;
        LINK: number;
        BTC: number;
    };
    comp: {
        getCompBalance: typeof comp.getCompBalance;
        getCompAccrued: typeof comp.getCompAccrued;
    };
};
...

0xbhagwan avatar Dec 06 '20 22:12 0xbhagwan

As it can be seen on the proposed fix, the (provider?: Provider | string, options?: CompoundOptions): CompoundInstance; is lost when using the spread operator.

Instead of trying to replicate the behaviour of Object.assign() while preserving types, I added some type definitions for the exported Compound object at src/index.ts, as well as for constants, and decimals at (provider?: Provider | string, options?: CompoundOptions): CompoundInstance;.

Since I was already adding types I also went ahead and wrote interfaces for Account and AccountReponse so that the example at https://compound.finance/docs/compound-js#account can run on strict mode without type issues. Using the new keywoard when initiallizing the compound instance is no longer an issue for the typescript compiler.

All changes can be seen at #10.

0xbhagwan avatar Dec 08 '20 15:12 0xbhagwan

is there a way to access this fix?

deeeed avatar Jul 25 '22 05:07 deeeed