-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to Option<T> #470
Comments
I'd like to say as a preference and also to match up with fp-ts and other functional programming styles, that in JS its nicer to have an type Option<T> = T | undefined; As is in more strict and no |
I think the issue with this is that the user will need to provide undefined value when don't want to pass anything. type Option<T> = T | undefined;
function foo(o: Option<number>) {
console.log(o);
}
foo(); |
The same occurs for a type with type PossiblyUndefined = number | undefined;
// or type PossiblyUndefined = number | null | undefined;
function bar(o: PossiblyUndefined) {
console.log(o);
}
bar(); But in original code, this should be more like: type Option<T> = T | undefined;
function foo(o: Option<number>) {
console.log(o);
}
let val: Option<number> = undefined;
foo(val);
// and
foo(undefined);
foo(undefined as Option<number>);
foo(1);
foo(2 as Option<number>)
// or change signature
function foo(o?: Option<number>) {
console.log(o);
}
// or
function foo(o: Option<number> = undefined) {
console.log(o);
} Which is verifying via the |
Yeah I think we need to make optional what will be mainly implemented on TypeChain. |
Description
Today if a contract uses the sway object
Option<T>
as a return or argument of a function, we parse it as a custom Enum from the ABI, requiring the user to take action and pass an object{ None: [] }
or{ Some: <value> }
to match the custom enum.The ideal implementation of
Option<T>
on typescript would be to abstract theenum Option
and;T
) or optional in this case, null or undefined.T
or null/undefined.Todos;
Option<T>
by identifying it from the json ABI.typechain
to match the implementation.Use case
Contract
Current way on Typescript
Desired implementation on Typescript
The text was updated successfully, but these errors were encountered: