near-api-js
near-api-js copied to clipboard
tsconfig questions
- Why is the source directory
src.ts
? I remember asking this months ago, but forget why. - Why allow for implicit any types?
- Why no strict null/undefined checking.
For example,
src/near.ts:31:13 - error TS2322: Type 'null' is not assignable to type 'AccountCreator'.
31 this.accountCreator = null;
And later it is checked for truthiness, which given the type it should always be true.
Another example:
async functionCall(contractId: string, methodName: string, args: any, gas?: BN, amount?: BN): Promise<FinalExecutionOutcome> {
args = args || {};
this.validateArgs(args);
return this.signAndSendTransaction(contractId, <span class="error">[functionCall(methodName, Buffer.from(JSON.stringify(args)), gas || DEFAULT_FUNC_CALL_GAS, amount)]</span>);
}
Here I assume that if amount
is undefined, it will be serialized into null. However, it is typed as defined in functionCall
.
Why is the source directory src.ts? I remember asking this months ago, but forget why.
No real reason, just to indicate that it's in TypeScript.
Why allow for implicit any types?
Because it's convenient. Writing out explicit any types adds zero useful information to either compiler or humans.
And later it is checked for truthiness, which given the type it should always be true.
Completely relying on TypeScript checking for whether value is null, etc is harmful. I think it is actually one of the biggest drawbacks of using TypeScript in general – it creates false sense of type safety. In real life every library would have mostly non-TypeScript usage and so runtime type of anything incoming isn't guaranteed. Developer experience can be improved a lot by having runtime checks which fail early as TypeScript unfortunately doesn't help with this at all (and makes problem worse by making it seem like such checks are unnecessary). @willemneal maybe you know of some babel plugin or smth like that which can actually enforce runtime checks based on TypeScript types?
Why no strict null/undefined checking.
We have far bigger problems with typings that this TBH. E.g. anything taking BN
is generally treated by users as taking BN
and/or string
(or maybe even number
) as well, see https://github.com/near/near-api-js/issues/220#issuecomment-584895790
Here I assume that if amount is undefined, it will be serialized into null. However, it is typed as defined in functionCall.
Not sure I understand the essence of the problem. Do you mean that passing undefined
and null
will result in different transaction data being signed?