Skip to content

Added language extensions page #20

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

Merged
merged 3 commits into from
Dec 27, 2020
Merged

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.

2 participants