-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Syntax suggestion: Ignore last void parameters #4260
Comments
Would I be correct in saying that the type signature |
Not always @weswigham, just in last arguments of functions. class X<T, U>
{
f(t: T|void, u: U) { ... }
}
I don't understand when you say Thanks |
Oh, sorry, I was (completely) mistaken - The equality's not true for optional function arguments, only optional record type members. :S (Optional function arguments in closure are denoted with an |
Chatting with @danquirk, our proposal (roughly) is that at a call expression, we treat the unprovided required arguments as if they were of type |
Yes @RyanCavanaugh, just for call expressions. I don't know how works typescript checker then I can't say if it's better don't throw error if last parameter is void and missing or if it's treat the unprovided required arguments as if they were of type If I can help you just tell what to do. |
Approved. Should be useful for |
@RyanCavanaugh What is the status on this issue? I saw that @Kingwl was working on a PR that was abandoned (#20642) with some spec issues raised by mhegazy. Anyway, I have a fix that works for the given examples as well as generic rest / tuple types here 9f6e28b if you want to breathe some life into this issue. @Kingwl : I didn't mean to conflict with your work (I didn't actually see your old PR until I finished my fix). If you want to take my work and finish the PR I would be fine with that. |
@jack-williams take it easy, I have already close the pr |
…ptional Fix #4260 : Allow trailing arguments that accept void to be omitted
@jack-williams It does not seem to work with constructors. Is this intentional or were constructors just forgotten? |
@FrogTheFrog Can you provide an example? |
@jack-williams Here you go: class Test<T1, T2 = void> {
constructor(input1: T1, input2: T2) {
// Do something
}
}
const test = new Test({}); I'm getting this error with test.ts:7:14 - error TS2554: Expected 2 arguments, but got 1.
7 const test = new Test({});
~~~~~~~~~~~~
test.ts:2:29
2 constructor(input1: T1, input2: T2) {
~~~~~~~~~~
An argument for 'input2' was not provided.
Found 1 error. |
@FrogTheFrog Thanks for the example. The issue is not to do with constructors; the issue is the generics. See this comment here in an older PR I made. For instance, this is fine: class VoidClass {
constructor(_x: void) {}
}
new VoidClass(); // ok Basically the special casing for Here is a workaround if you really want it: class Test<T1, T2 = void> {
constructor(input1: T1, input2: T2) {
// Do something
}
static makeFactory<T1, T2 = void>(): (input1: T1, input2: T2) => Test<T1,T2> {
return (input1: T1, input2: T2) => new Test(input1, input2);
}
}
const factory = Test.makeFactory<number>();
const factoryTwo = Test.makeFactory<number, number>();
const testOne = factory(3); // ok
const testTwo = factoryTwo(3); // not ok |
@jack-williams I'm just nitpicking at this point. Since I mainly use "options" object for public classes, it does not really bother me. I'm using this simple trick fallback to interface TestOptions<T1, T2> {
input1: T1;
input2?: T2;
}
class Test<T1, T2 = void> {
constructor(options: TestOptions<T1, T2>) {
if (options.input2 !== void 0) {
// Do something with input2
}
}
}
const test = new Test({ input1: {} }); //=> test has Test<{}, void> type I'm just glad that the |
Why does this fail? Playground. function qwe<T = void>(a: T) {}
qwe(); // ERROR: Expected 1 arguments, but got 0. function qwe<void>(a: void): void
function abc(a: void) {}
abc(); // Passes |
The checks are done before |
test case: tests/cases/compiler/genericObjectLitReturnType.ts
maybe the solution is the same for #3356
The text was updated successfully, but these errors were encountered: