Skip to content

Commit

Permalink
Better type safety for defineReadOnly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Feb 4, 2020
1 parent ff9bc2a commit e7adc84
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Interface {

readonly _abiCoder: AbiCoder;

static _isInterface: boolean;
readonly _isInterface: boolean;

constructor(fragments: string | Array<Fragment | JsonFragment | string>) {
logger.checkNew(new.target, Interface);
Expand Down Expand Up @@ -87,7 +87,7 @@ export class Interface {
logger.warn("duplicate definition - constructor");
return;
}
defineReadOnly(this, "deploy", fragment);
defineReadOnly(this, "deploy", <ConstructorFragment>fragment);
return;
case "function":
bucket = this.functions;
Expand Down
12 changes: 6 additions & 6 deletions packages/asm/src.ts/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export abstract class Node {
ethers.utils.defineReadOnly(this, "warnings", [ ]);

for (const key in options) {
ethers.utils.defineReadOnly(this, key, options[key]);
ethers.utils.defineReadOnly<any, any>(this, key, options[key]);
}
}

Expand Down Expand Up @@ -702,11 +702,11 @@ class Assembler {

readonly defines: { [ name: string ]: any };

private _stack: Array<Node>;
private _parents: { [ tag: string ]: Node };
private _script: Script;
_stack: Array<Node>;
_parents: { [ tag: string ]: Node };
_script: Script;

private _changed: boolean;
_changed: boolean;

constructor(root: Node, options: AssemblerOptions) {
ethers.utils.defineReadOnly(this, "positionIndependentCode", !!options.positionIndependentCode);
Expand All @@ -718,7 +718,7 @@ class Assembler {


const nodes: { [ tag: string ]: NodeState } = { };
const labels: { [ name: string ]: Node } = { };
const labels: { [ name: string ]: LabelledNode } = { };
const parents: { [ tag: string ]: Node } = { };

// Link labels to their target node
Expand Down
4 changes: 2 additions & 2 deletions packages/basex/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class BaseX {
readonly alphabet: string;
readonly base: number;

private _alphabetMap: { [ character: string ]: number };
private _leader: string;
_alphabetMap: { [ character: string ]: number };
_leader: string;

constructor(alphabet: string) {
defineReadOnly(this, "alphabet", alphabet);
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src.ts/bin/ethers-ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,12 @@ abstract class AccountPlugin extends EnsPlugin {
}
async _setValue(key: string, value: string): Promise<void> {
ethers.utils.defineReadOnly(this, key, value);
ethers.utils.defineReadOnly<any, any>(this, key, value);
if (key === "name") {
await this._setValue("nodehash", ethers.utils.namehash(value));
}
}
async prepareOptions(argParser: ArgParser): Promise<void> {
await super.prepareOptions(argParser);
ethers.utils.defineReadOnly(this, "_wait", argParser.consumeFlag("wait"));
}
async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src.ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ export abstract class Plugin {
network: ethers.providers.Network;
provider: ethers.providers.Provider;

accounts: Array<WrappedSigner>;
accounts: ReadonlyArray<WrappedSigner>;
mnemonicPassword: boolean;
_xxxMnemonicPasswordHard: boolean;

Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export class Contract {
const uniqueFilters: { [ name: string ]: Array<string> } = { };
Object.keys(this.interface.events).forEach((eventSignature) => {
const event = this.interface.events[eventSignature];
defineReadOnly(this.filters, eventSignature, (...args: Array<any>) => {
defineReadOnly<any, any>(this.filters, eventSignature, (...args: Array<any>) => {
return {
address: this.address,
topics: this.interface.encodeFilterTopics(event, args)
Expand Down Expand Up @@ -526,7 +526,7 @@ export class Contract {
const run = runMethod(this, name, { });

if (this[name] == null) {
defineReadOnly(this, name, run);
defineReadOnly<any, any>(this, name, run);
}

if (this.functions[name] == null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/properties/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

export function defineReadOnly(object: any, name: string, value: any): void {
export function defineReadOnly<T, K extends keyof T>(object: T, name: K, value: T[K]): void {
Object.defineProperty(object, name, {
enumerable: true,
value: value,
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/src.ts/fallback-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function getRunner(provider: Provider, method: string, params: { [ key: string]:
}

export class FallbackProvider extends BaseProvider {
readonly providerConfigs: Array<FallbackProviderConfig>;
readonly providerConfigs: ReadonlyArray<FallbackProviderConfig>;
readonly quorum: number;

// Due to teh highly asyncronous nature of the blockchain, we need
Expand Down
3 changes: 2 additions & 1 deletion packages/providers/src.ts/url-json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

"use strict";

import { Network, Networkish } from "@ethersproject/networks";
Expand Down Expand Up @@ -30,7 +31,7 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
defineReadOnly(this, "apiKey", apiKey);
} else if (apiKey != null) {
Object.keys(apiKey).forEach((key) => {
defineReadOnly(this, key, apiKey[key]);
defineReadOnly<any, any>(this, key, apiKey[key]);
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions packages/wallet/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class Wallet extends Signer implements ExternallyOwnedAccount {
}
} else {
defineReadOnly(this, "_mnemonic", (): Mnemonic => null);
defineReadOnly(this, "path", null);
}


Expand All @@ -82,7 +81,6 @@ export class Wallet extends Signer implements ExternallyOwnedAccount {
defineReadOnly(this, "_signingKey", () => signingKey);
}
defineReadOnly(this, "_mnemonic", (): Mnemonic => null);
defineReadOnly(this, "path", null);
defineReadOnly(this, "address", computeAddress(this.publicKey));
}

Expand Down

0 comments on commit e7adc84

Please sign in to comment.