Skip to content

adding "Importing Arrays" section #59

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 4 commits into from
Aug 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions docs/external-lua-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,78 @@ You can simply add a Lua file as part of your project sources if you add [a decl
Your project should look like:

```
main.ts
somelua.lua
somelua.d.ts
tsconfig.json
project/
├── main.ts
├── someLua.lua
├── someLua.d.ts
└── tsconfig.json
```

And you can use it like so:

```ts title=main.ts
import { myFunction } from "./somelua";
import { foo, bar } from "./someLua";

foo();
bar();
```

```lua title=someLua.lua
local someLua = {}

function someLua:foo()
print("hello")
end

function someLua:bar()
print("world")
end

return someLua
```

```ts title=someLua.d.ts
export function foo(): void;
export function bar(): void;
```

## Importing Arrays

myFunction();
Building on the previous section, you might want also want to import a Lua array. For example:

```lua title=things.lua
return {
{
"foo": 123,
"bar": 456,
},
{
"foo": 789,
"bar": 987,
},
}
```

In normal TypeScript code that imports an array, you would typically use the `export default` functionality of ES6 imports. But you can't do that here, because Lua code has no import named `default`. Instead, you have to use `export =` syntax, like so:
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect, we support export/import default functionality just fine, it's just that your lua file does not return a module (table) with a member named default


```ts title=things.d.ts
interface Thing {
foo: number;
bar: number;
}

declare const things: Thing[];
export = things;
```

Then, in your TypeScript code, you can import it exactly like you would expect:

```ts title=main.ts
import contents from "./module";
Copy link
Member

Choose a reason for hiding this comment

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

Is this actually correct? I thought this should be import * as contents from "./module";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upon further investigation, you're right, but it depends on whether or not you have --esModuleInterop flag turned on. I'll change it to be import *.

```

Finally, note that for this to work, `esModuleInterop` must be specified as true in your `tsconfig.json` file.
Copy link
Member

Choose a reason for hiding this comment

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

Have my doubts if this line should be in here, tstl actually does not look at this option at all, if this needs to have a certain value it would be purely to keep TS happy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok i removed it


For more information about this export syntax, see [the official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require).

## Using NPM packages

To use a Lua package, install it via npm and use it as you would for any regular npm package in TypeScript. If the package does not include its own `.d.ts` declaration files, you can create your own by adding a `<package name>.d.ts` [declaration file](./advanced/writing-declarations.md) to your source files.
Expand Down