Better type safety for defineReadOnly.

This commit is contained in:
Richard Moore 2020-02-04 08:01:26 -05:00
parent ff9bc2a282
commit e7adc84a97
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
10 changed files with 18 additions and 25 deletions

@ -55,7 +55,7 @@ export class Interface {
readonly _abiCoder: AbiCoder; readonly _abiCoder: AbiCoder;
static _isInterface: boolean; readonly _isInterface: boolean;
constructor(fragments: string | Array<Fragment | JsonFragment | string>) { constructor(fragments: string | Array<Fragment | JsonFragment | string>) {
logger.checkNew(new.target, Interface); logger.checkNew(new.target, Interface);
@ -87,7 +87,7 @@ export class Interface {
logger.warn("duplicate definition - constructor"); logger.warn("duplicate definition - constructor");
return; return;
} }
defineReadOnly(this, "deploy", fragment); defineReadOnly(this, "deploy", <ConstructorFragment>fragment);
return; return;
case "function": case "function":
bucket = this.functions; bucket = this.functions;

@ -155,7 +155,7 @@ export abstract class Node {
ethers.utils.defineReadOnly(this, "warnings", [ ]); ethers.utils.defineReadOnly(this, "warnings", [ ]);
for (const key in options) { for (const key in options) {
ethers.utils.defineReadOnly(this, key, options[key]); ethers.utils.defineReadOnly<any, any>(this, key, options[key]);
} }
} }
@ -702,11 +702,11 @@ class Assembler {
readonly defines: { [ name: string ]: any }; readonly defines: { [ name: string ]: any };
private _stack: Array<Node>; _stack: Array<Node>;
private _parents: { [ tag: string ]: Node }; _parents: { [ tag: string ]: Node };
private _script: Script; _script: Script;
private _changed: boolean; _changed: boolean;
constructor(root: Node, options: AssemblerOptions) { constructor(root: Node, options: AssemblerOptions) {
ethers.utils.defineReadOnly(this, "positionIndependentCode", !!options.positionIndependentCode); ethers.utils.defineReadOnly(this, "positionIndependentCode", !!options.positionIndependentCode);
@ -718,7 +718,7 @@ class Assembler {
const nodes: { [ tag: string ]: NodeState } = { }; const nodes: { [ tag: string ]: NodeState } = { };
const labels: { [ name: string ]: Node } = { }; const labels: { [ name: string ]: LabelledNode } = { };
const parents: { [ tag: string ]: Node } = { }; const parents: { [ tag: string ]: Node } = { };
// Link labels to their target node // Link labels to their target node

@ -45,8 +45,8 @@ export class BaseX {
readonly alphabet: string; readonly alphabet: string;
readonly base: number; readonly base: number;
private _alphabetMap: { [ character: string ]: number }; _alphabetMap: { [ character: string ]: number };
private _leader: string; _leader: string;
constructor(alphabet: string) { constructor(alphabet: string) {
defineReadOnly(this, "alphabet", alphabet); defineReadOnly(this, "alphabet", alphabet);

@ -241,18 +241,12 @@ abstract class AccountPlugin extends EnsPlugin {
} }
async _setValue(key: string, value: string): Promise<void> { 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") { if (key === "name") {
await this._setValue("nodehash", ethers.utils.namehash(value)); 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> { async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args); await super.prepareArgs(args);

@ -491,7 +491,7 @@ export abstract class Plugin {
network: ethers.providers.Network; network: ethers.providers.Network;
provider: ethers.providers.Provider; provider: ethers.providers.Provider;
accounts: Array<WrappedSigner>; accounts: ReadonlyArray<WrappedSigner>;
mnemonicPassword: boolean; mnemonicPassword: boolean;
_xxxMnemonicPasswordHard: boolean; _xxxMnemonicPasswordHard: boolean;

@ -477,7 +477,7 @@ export class Contract {
const uniqueFilters: { [ name: string ]: Array<string> } = { }; const uniqueFilters: { [ name: string ]: Array<string> } = { };
Object.keys(this.interface.events).forEach((eventSignature) => { Object.keys(this.interface.events).forEach((eventSignature) => {
const event = this.interface.events[eventSignature]; const event = this.interface.events[eventSignature];
defineReadOnly(this.filters, eventSignature, (...args: Array<any>) => { defineReadOnly<any, any>(this.filters, eventSignature, (...args: Array<any>) => {
return { return {
address: this.address, address: this.address,
topics: this.interface.encodeFilterTopics(event, args) topics: this.interface.encodeFilterTopics(event, args)
@ -526,7 +526,7 @@ export class Contract {
const run = runMethod(this, name, { }); const run = runMethod(this, name, { });
if (this[name] == null) { if (this[name] == null) {
defineReadOnly(this, name, run); defineReadOnly<any, any>(this, name, run);
} }
if (this.functions[name] == null) { if (this.functions[name] == null) {

@ -4,7 +4,7 @@ import { Logger } from "@ethersproject/logger";
import { version } from "./_version"; import { version } from "./_version";
const logger = new Logger(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, { Object.defineProperty(object, name, {
enumerable: true, enumerable: true,
value: value, value: value,

@ -298,7 +298,7 @@ function getRunner(provider: Provider, method: string, params: { [ key: string]:
} }
export class FallbackProvider extends BaseProvider { export class FallbackProvider extends BaseProvider {
readonly providerConfigs: Array<FallbackProviderConfig>; readonly providerConfigs: ReadonlyArray<FallbackProviderConfig>;
readonly quorum: number; readonly quorum: number;
// Due to teh highly asyncronous nature of the blockchain, we need // Due to teh highly asyncronous nature of the blockchain, we need

@ -1,3 +1,4 @@
"use strict"; "use strict";
import { Network, Networkish } from "@ethersproject/networks"; import { Network, Networkish } from "@ethersproject/networks";
@ -30,7 +31,7 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
defineReadOnly(this, "apiKey", apiKey); defineReadOnly(this, "apiKey", apiKey);
} else if (apiKey != null) { } else if (apiKey != null) {
Object.keys(apiKey).forEach((key) => { Object.keys(apiKey).forEach((key) => {
defineReadOnly(this, key, apiKey[key]); defineReadOnly<any, any>(this, key, apiKey[key]);
}); });
} }
} }

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