-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix optional arguments in TypeScript #1483
Conversation
I assumed so, but when I run those tests (with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
Some notes: I also adjusted crates/typescript-tests/src/opt_args_and_ret.ts, but I could not find out whether and where this file is actually used. I think it's dead code, but I might be wrong.
It is used for local and CI tests.
fn optional(name: String, type_: String) -> Self { | ||
Self { optional: true, name, type_ } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the names of these methods be changed to new_(required|optional)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any guidelines / recommendations on this topic? I couldn't find anything in https://github.com/rust-dev-tools/fmt-rfcs/.
I personally prefer the shorter version, it's clear from the signature that it's a constructor.
Apply feedback about omittable and non-omittable arguments. Thanks @rhysd!
Update tests
671f193
to
608a819
Compare
PR updated! @alexcrichton I added some tests, but note that the tests are a bit flaky with regards to ensuring correct output for optional and/or omittable parameters. I found several combinations that pass the test but are not actually correct. For example, this works: // typescript_tests.d.ts
export function opt_fn_mixed(_a: number | undefined, _b: number, _c?: number): number | undefined;
// opt_args_and_ret.ts
const opt_fn_mixed: (a: number, b: number, c: number) => number | undefined = wbg.opt_fn_mixed; The reason is that I think a test function or macro that asserts the correctly generated TS output string/file would be the better approach than using TypeScript's type system for verification. But that's out of scope for this PR. |
Ok no worries, this looks good to me! Sorry for the delay in reviewing :( I'm fine testing more in further PRs as well, this certainly looks good enough for now! @dbrgn want to open a follow-up issue with your thoughts on testing? |
For a function like this:
...the currently generated typescript looks like this:
Support for
undefined
in the parameter type was added in #1201. However, while typenumber | undefined
is technically optional at runtime, typechecking tools will still consider the parameter to be required. (CC @rhysd, please correct me if this information is actually incorrect.)Instead, the TypeScript should look like this, with a
?
appended to the parameter name:Reference: https://www.typescriptlang.org/docs/handbook/functions.html#optional-and-default-parameters
This PR should actually fix #1129.
Some notes: I also adjusted
crates/typescript-tests/src/opt_args_and_ret.ts
, but I could not find out whether and where this file is actually used. I think it's dead code, but I might be wrong.