question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript typings are wrong.

See original GitHub issue

https://github.com/trufflesuite/ganache-core/blob/bda6a4ae07400dc5567aa50776e4432f96d3ae1e/typings/index.d.ts#L1-L48

The TypeScript type definitions for this project appear to be incorrect. If I do import ganache from 'ganache-core' my project will compile without trouble but will fail at runtime due to ganache being undefined. However, if I instead do import * as ganache from 'ganache-core' or import { server } from 'ganache-core' I get compile-time errors but the script runs at runtime.

I believe the proper definition should look something like this instead:

declare module 'ganache-core' {
	import { Provider as Web3Provider } from "web3/providers";
	export interface IProviderOptions {
		account_keys_path?: string;
		accounts?: object[];
		allowUnlimitedContractSize?: boolean;
		blockTime?: number;
		db_path?: string;
		debug?: boolean;
		default_balance_ether?: number;
		fork?: string | object;
		fork_block_number?: string | number;
		gasLimit?: number;
		gasPrice?: string;
		hardfork?: "byzantium" | "constantinople" | "petersburg";
		hd_path?: string;
		locked?: boolean;
		logger?: {
			log(msg: string): void;
		};
		mnemonic?: string;
		network_id?: number;
		networkId?: number;
		port?: number;
		seed?: any;
		time?: Date;
		total_accounts?: number;
		unlocked_accounts?: string[];
		verbose?: boolean;
		vmErrorsOnRPCResponse?: boolean;
		ws?: boolean;
	}

	export interface IServerOptions extends IProviderOptions {
		keepAliveTimeout?: number;
	}

	export function provider(opts?: IProviderOptions): Provider;
	export function server(opts?: IServerOptions): any;

	export interface Provider extends Web3Provider {
		close: (callback: Function) => void;
	}
}

Do the current typings actually work for anyone? If so, how are you doing your imports? If not, I can submit a PR with the above change.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
davidmurdochcommented, Oct 25, 2020

Yes, IProviderOptions is only type, and doesn’t have a corresponding runtime component, whereas provider and server are both types AND have a runtime component (the function). When coding, TypeScript recognizes if you are writing runtime code or type code, and only gives you appropriate hints.

examples:

// runtime
const doesntMakeSense = Ganache.IProviderOptions; // would be `undefined` since this is runtime code, a `IProviderOptions` doesn't have a runtime component

type myType = Ganache.IProviderOptions // works because this is type-level code
let myThing: Ganache.IProviderOptions = {gasPrice: 123456}; // works because this is also type-level code

I’m sure there is a better way of explaining it, but hopefully this helps clear things up for you.

1reaction
zemsecommented, Oct 25, 2020

and all I get is provider and server in my VSCode auto-complete

I have observed that when you type next to a : of a var while specifying type, it gives more suggestions.

import Ganache from 'ganache-core';

const variable: Ganache. // gives more suggestions like IProviderOptions

Hope this helps

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - Handling wrong package typings - Stack Overflow
I am using a JavaScript library which is published via NPM along with TypeScript typings. Unfortunately these typings are often wrong.
Read more >
TypeScript typings are not available due to wrong field in ...
Hi. Whenever I import something from this package in my TypeScript project, I get an error saying that the declarations could not be...
Read more >
You Might Be Using Typescript Wrong... - YouTube
I'm getting increasingly tired of the "but TS is so much woooooork" comments so I figured I'd do a rant.If you have friends...
Read more >
Handbook - Basic Types - TypeScript
The most basic datatype is the simple true/false value, which JavaScript and TypeScript call a boolean value. ts. let isDone : boolean =...
Read more >
TypeScript Programming with Visual Studio Code
Or, if the built-in formatter is getting in the way, set "typescript.format.enable" to false to disable it. For more specialized code formatting styles,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found