Skip to content

Conversation

@mcmire
Copy link
Contributor

@mcmire mcmire commented Mar 18, 2022

Some of our internal NPM packages are still untyped. Currently we get
around this by stubbing the types for these modules in a
dependencies.d.ts file, but this merely tags everything exported from
the modules as any.

To address this, this commit adds proper types for most of the modules
marked in dependencies.d.ts so that these types can be accessed and
utilized within the code that uses these modules. The modules that are
left over are those that would either take too long to fill in or those
that we are planning on removing soon anyway.


NOTE: These changes are not intended to be breaking. In other words, all the types here should reflect current behavior, not ideal behavior. If you see a change that could be breaking, let me know.


Fixes #728.

@mcmire mcmire requested a review from a team as a code owner March 18, 2022 23:14
AccountTrackerState
> {
private ethQuery: any;
private ethQuery: EthQuery | null;
Copy link
Contributor Author

@mcmire mcmire Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property can be null because it is not set in the constructor (or at least I didn't see that it was set in the constructor; I may be wrong).

const { erc20, logo: filePath, ...token } = (contractMap as ContractMap)[
tokenAddress
];
const { erc20, logo: filePath, ...token } = contractMap[tokenAddress];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types were added in this PR for @metamask/contract-metadata, so typecasting is no longer necessary.

const latestBlockNumber = 3;
const numberOfRequestedBlocks = 3;

beforeEach(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been there all along and for some reason it was not causing the test to fail.

includeNextBlock = false,
}: {
ethQuery: EthQuery;
ethQuery: EthQueryish;
Copy link
Contributor Author

@mcmire mcmire Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An "EthQueryish" is an object that looks like an EthQuery without the full gamut of methods that EthQuery supports. It's the type of the argument that query now takes, and since it only really cares that the object has a sendAsync method, that's what this type checks for.


const txNonce =
nonce ||
(await query(this.ethQuery, 'getTransactionCount', [from, 'pending']));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changed because there is now a new type parameter on query that governs the type of data that it returns (as unfortunately due to the generic nature of query, we can't figure it out). The behavior is the same though, these are just type annotations.


it('bNToHex', () => {
expect(util.BNToHex(new BN('1337'))).toBe('0x539');
describe('BNToHex', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the tests on this function because I wanted to make sure it did what I expected and I found the tests lacking.

*/
export function weiHexToGweiDec(hex: string) {
const hexWei = new BN(stripHexPrefix(hex), 16);
return fromWei(hexWei, 'gwei').toString(10);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromWei already returns a string; this is unnecessary.

@mcmire mcmire force-pushed the backfill-types branch 3 times, most recently from 633d7fb to 3929f6c Compare March 19, 2022 00:00
collectCoverageFrom: ['./src/**/*.ts'],
// TODO: Test index.ts
coveragePathIgnorePatterns: ['./src/index.ts'],
coveragePathIgnorePatterns: ['./src/index.ts', './src/gas/gas-util.ts'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My changes to this file resulted in the coverage percentage falling below our threshold, so I've disabled this file for the time being. I logged an issue to backfill these tests here: #734

@mcmire mcmire force-pushed the fix-tsconfig-for-tests branch 2 times, most recently from d130fde to ecd3eea Compare March 25, 2022 19:56
Base automatically changed from fix-tsconfig-for-tests to main March 30, 2022 20:06
A previous commit updated the TypeScript configuration so TypeScript
would better recognize type definition files whose purpose was to
backfill types for existing packages. After testing, this revealed a
misunderstanding of the `paths` option in `tsconfig.json`.

It turns out that `paths` completely overrides how TypeScript resolves
modules and locates type definition files for modules. The consequence
of this option is that if a module includes type definitions, but we
also supply type definitions for that module in `types/`, then our type
definitions will win. This degrades the developer experience: sometimes,
a package already has types, but we merely need to *augment* those
types in some way. The `paths` option makes it impossible to do this.

This commit keeps the `types/` directory (as it'll be important later)
but informs TypeScript about the type definition files here by adding
them to the `include` option in `tsconfig.json` (which was the original
strategy).
@mcmire mcmire changed the base branch from main to fix-tsconfig March 30, 2022 22:25
Some of our internal NPM packages are still untyped. Currently we get
around this by stubbing the types for these modules in a
`dependencies.d.ts` file, but this merely tags everything exported from
the modules as `any`.

To address this, this commit adds proper types for most of the modules
marked in `dependencies.d.ts` so that these types can be accessed and
utilized within the code that uses these modules. The modules that are
left over are those that would either take too long to fill in or those
that we are planning on removing soon anyway.
@mcmire
Copy link
Contributor Author

mcmire commented Mar 30, 2022

I realized there were some issues with the changes I made in #732, so this PR is now based on a new PR, #761, which addresses those issues.

@mcmire
Copy link
Contributor Author

mcmire commented Mar 31, 2022

As explained in #761, I don't think adding ambient types for these packages to this repo is the right way to go about this. I think it would make more sense (and would also be more straightforward) to just add types directly to these packages (or add the types to the DefinitelyTyped repo for ones that we don't fully control). Thoughts/comments?

Base automatically changed from fix-tsconfig to main April 5, 2022 20:35
const { ethQuery } = this;

if (ethQuery === null) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a change in behavior. It now returns nothing instead of throwing an error.

The same goes for syncBalanceWithAddresses - it now returns an empty object rather than throwing.

@mcmire
Copy link
Contributor Author

mcmire commented Apr 13, 2022

I'm still going the route of adding types to our packages directly rather than use ambient types as I think that will be better in the end, so I'll close this, and we can use this for inspiration later if we really need to revive this.

@mcmire mcmire closed this Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backfill TypeScript types for various dependencies

3 participants