Skip to content

Conversation

@Perryvw
Copy link
Member

@Perryvw Perryvw commented Dec 26, 2020

No description provided.

```

:::note
Prefer MultiReturn over the similar [@tupleReturn anotation](./compiler-annotations.md#tuplereturn). MultiReturn can do anything tupleReturn can, with the added benefit of being able to distinguish between actual tuple tables and multiple return values in the type system.

This comment was marked as resolved.


```ts title=stringfind.ts
declare namespace string {
export function find(haystack: string, needle: string): MultiReturn<[number, number]>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if someone will question why this doesn't work the same in the future.

this: void or @noSelf would be required. Unless the namespace is defined elsewhere with that very annotation like it is in the playground

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no it works because string has @noSelfInFile


import { SideBySide } from "@site/src/components/SideBySide";

TypeScriptToLua provides several extensions to typescript in the form of types and helper functions. To use these language extensions, add the types to your `tsconfig.json`:

This comment was marked as resolved.


## MultiReturn Type

This language extension allows typing of Lua functions that return multiple values. For example, consider Lua's `string.find`, it returns two indices: the start of the found substring and the end of the found substring. In TypeScript, function can only return one value so a special type is needed to indicate to tstl there are multiple return values. This is the `MultiReturn<>` type.

This comment was marked as resolved.

@Perryvw Perryvw merged commit 17c8cc3 into source Dec 27, 2020
@Perryvw Perryvw deleted the feature/language-extensions branch December 27, 2020 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants