From 6c98e5b4f6d84a517b490e98b728244bbeb65677 Mon Sep 17 00:00:00 2001 From: Zamiell <5511220+Zamiell@users.noreply.github.com> Date: Tue, 3 Aug 2021 17:08:51 -0400 Subject: [PATCH 1/4] adding "Importing Arrays" section --- docs/external-lua-code.md | 71 +++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/docs/external-lua-code.md b/docs/external-lua-code.md index fe157d75..4588303e 100644 --- a/docs/external-lua-code.md +++ b/docs/external-lua-code.md @@ -11,20 +11,77 @@ 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 { myFunction } from "./someLua"; myFunction(); ``` +```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 + +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: + +```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"; +``` + +Finally, note that for this to work, `esModuleInterop` must be specified as true in your `tsconfig.json` file. + +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 `.d.ts` [declaration file](./advanced/writing-declarations.md) to your source files. From efd845b344f4e5e51a742591360faec6517316e4 Mon Sep 17 00:00:00 2001 From: James <5511220+Zamiell@users.noreply.github.com> Date: Wed, 4 Aug 2021 17:06:57 -0400 Subject: [PATCH 2/4] Update external-lua-code.md --- docs/external-lua-code.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/external-lua-code.md b/docs/external-lua-code.md index 4588303e..ca80e1bd 100644 --- a/docs/external-lua-code.md +++ b/docs/external-lua-code.md @@ -19,9 +19,10 @@ project/ ``` ```ts title=main.ts -import { myFunction } from "./someLua"; +import { foo, bar } from "./someLua"; -myFunction(); +foo(); +bar(); ``` ```lua title=someLua.lua From 834d39a9c1844c8704c88c99769205bec7137779 Mon Sep 17 00:00:00 2001 From: James <5511220+Zamiell@users.noreply.github.com> Date: Wed, 4 Aug 2021 17:14:18 -0400 Subject: [PATCH 3/4] Update external-lua-code.md --- docs/external-lua-code.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/external-lua-code.md b/docs/external-lua-code.md index ca80e1bd..22ac05bd 100644 --- a/docs/external-lua-code.md +++ b/docs/external-lua-code.md @@ -76,10 +76,10 @@ export = things; Then, in your TypeScript code, you can import it exactly like you would expect: ```ts title=main.ts -import contents from "./module"; -``` +import * as contents from "./module"; -Finally, note that for this to work, `esModuleInterop` must be specified as true in your `tsconfig.json` file. +print(contents[0]); +``` For more information about this export syntax, see [the official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require). From a6f73254c864361e00324d3b20ebd756b7100fa4 Mon Sep 17 00:00:00 2001 From: Zamiell <5511220+Zamiell@users.noreply.github.com> Date: Wed, 4 Aug 2021 17:31:48 -0400 Subject: [PATCH 4/4] rewriting --- docs/external-lua-code.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/external-lua-code.md b/docs/external-lua-code.md index 22ac05bd..ceea3757 100644 --- a/docs/external-lua-code.md +++ b/docs/external-lua-code.md @@ -44,24 +44,24 @@ export function foo(): void; export function bar(): void; ``` -## Importing Arrays +## Importing a Lua module that only exports an array -Building on the previous section, you might want also want to import a Lua array. For example: +Building on the previous section, you might want also want to import a Lua file that only exports an array. For example, something like: ```lua title=things.lua return { { - "foo": 123, - "bar": 456, + foo = 123, + bar = 456, }, { - "foo": 789, - "bar": 987, + 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: +Writing a definitions file for this is tricky, since the Lua file has no named imports and no default export. Here, you have to use `export =` syntax, like so: ```ts title=things.d.ts interface Thing { @@ -73,12 +73,12 @@ declare const things: Thing[]; export = things; ``` -Then, in your TypeScript code, you can import it exactly like you would expect: +Then, in your TypeScript code, you can import it like: ```ts title=main.ts -import * as contents from "./module"; +import * as things from "./module"; -print(contents[0]); +print(things[0].foo); // Prints "123" ``` For more information about this export syntax, see [the official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require).