Conversation
|
I think |
I agree "helper" is too generic. but I don't think "macros" is the correct term. Personally I would prefer "extension". It is still a very generic term, but that's the term used for c++ compiler/language extensions: (https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/C_002b_002b-Extensions.html , https://docs.microsoft.com/en-us/cpp/build/reference/microsoft-extensions-to-c-and-cpp?view=msvc-160) |
As discussed on discord, let's go with language-extensions if @hazzard993 agrees: |
|
In regards to #947, type ToArray<T> = T extends any[] ? T : [T];
declare function pcall<F extends (...args: any[]) => any>(
this: void,
cb: F,
...args: Parameters<F>
): MultiReturn<
| [false, string, ...undefined[]]
| [true, ...ToArray<ReturnType<F>>]
>;It looks like TypeScript needs a bit more work to assign the typings correctly const [result, a, b, c] = pcall((a: number) => {
return $multi(1, 2, 3);
}, 0);
result; // boolean
a; // string | number | true (expected string | number)
b; // string | number | boolean | undefined (expected number | undefined)
c; // string | number | boolean | undefined (expected number | undefined) |
Perryvw
left a comment
There was a problem hiding this comment.
Changes look good, would like you to add one or a couple extra tests (see comments)
|
Is |
|
@LoganDark From the few experiments I did it does not really matter, I was hoping one would provide proper type narrowing, but seems like for now neither do that. Maybe we can come up with a way to update the type later. For now I would probably go with the second option personally, but you can do whatever you prefer. |
The second one does provide the benefit of being definitely wrapped in exactly one outer |
|
There are issues:
|
|
Instead of wrapping the right side you can also do it on the left instead: |
Yeah, I suppose that's reasonable. It's still annoying that I have to spread it, though. I expected it to act just like the tuples did before. And that invalid code generation is still probably a bug. |
For #580
Giving a
helperlanguage-extension implementation a try.This PR makes TypeScriptToLua declare the
$multifunction andMultiReturntype. These can be used as a type safe alternatives to@tupleReturn.You'll be able to use something like the
tsconfig.jsonbelow to get access to$multi.{ "compilerOptions": { "types": [ "typescript-to-lua/language-extensions" ] } }This function can only be used in a
ReturnStatementor as a return expression in anArrowFunction.