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

View File

@ -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);
@ -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;

View File

@ -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]);
}
}
@ -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);
@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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)
@ -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) {

View File

@ -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,

View File

@ -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

View File

@ -1,3 +1,4 @@
"use strict";
import { Network, Networkish } from "@ethersproject/networks";
@ -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]);
});
}
}

View File

@ -67,7 +67,6 @@ export class Wallet extends Signer implements ExternallyOwnedAccount {
}
} else {
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, "_mnemonic", (): Mnemonic => null);
defineReadOnly(this, "path", null);
defineReadOnly(this, "address", computeAddress(this.publicKey));
}