You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I mentioned this in a controllers PR, but I believe that in order to provide proper type safety, each object should have as const on it. That is to say:
exportconstabiERC1155=[
...
]asconst;
Why? Say you have a function or method and you want to ensure that it must take an ERC-1155 ABI. You might think you can say:
But this won't do what you think it will, because the type of the ABI object doesn't verify that you've passed an ERC-1155 ABI exactly, just something that matches the shape.
Let's simplify this:
constvalue={foo: "bar",baz: "qux",};
The inferred type of this is:
{
foo: string;
baz: string;}
That means that if you have a function foo(x: typeof value), this function will allow foo({ foo: "xxxxx", bar: "yyyyyy" }). Probably not what you want.
But if you add as const you can harden this. The inferred type of:
constvalue={foo: "bar",baz: "qux",}asconst;
is:
{
foo: "bar";
baz: "qux";}
So now foo({ foo: "xxxxx", bar: "yyyyyy" }) won't work anymore, it must be foo({ foo: "bar", baz: "qux" }). The same thing could be done for the ABI objects.
Alternatively, if we don't care about hardening the ABI like this then it might be good to make an ABI type.
The text was updated successfully, but these errors were encountered:
I mentioned this in a
controllers
PR, but I believe that in order to provide proper type safety, each object should haveas const
on it. That is to say:Why? Say you have a function or method and you want to ensure that it must take an ERC-1155 ABI. You might think you can say:
But this won't do what you think it will, because the type of the ABI object doesn't verify that you've passed an ERC-1155 ABI exactly, just something that matches the shape.
Let's simplify this:
The inferred type of this is:
That means that if you have a function
foo(x: typeof value)
, this function will allowfoo({ foo: "xxxxx", bar: "yyyyyy" })
. Probably not what you want.But if you add
as const
you can harden this. The inferred type of:is:
So now
foo({ foo: "xxxxx", bar: "yyyyyy" })
won't work anymore, it must befoo({ foo: "bar", baz: "qux" })
. The same thing could be done for the ABI objects.Alternatively, if we don't care about hardening the ABI like this then it might be good to make an ABI type.
The text was updated successfully, but these errors were encountered: