-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexport-types.ts
More file actions
103 lines (93 loc) · 2.62 KB
/
Copy pathexport-types.ts
File metadata and controls
103 lines (93 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { DocFuncBuilder, DocFuncBuilderParam, DocFuncBuilderType } from "./doc-builders";
/**
* Represents a LuaDoc (sumneko) type or a TypeScript type.
*/
export interface ExportableType {
asLua: () => string;
asTs: () => string;
}
/**
* Represents literal type strings across Lua and TS that are the same !
*/
export class LiteralExportable implements ExportableType {
constructor(private literal: string) {}
asLua() {
return this.literal;
}
asTs() {
return this.literal;
}
}
/**
* Represents a type that exports Lua and TS independently of each other
*/
export class IndependentLiteralExportable implements ExportableType {
constructor(private op: { lua: string; ts: string }) {}
asLua() {
return this.op.lua;
}
asTs() {
return this.op.ts;
}
}
export class FunctionExportable extends DocFuncBuilder implements ExportableType {
constructor(parameters?: DocFuncBuilderParam[], returnType?: DocFuncBuilderType) {
super(null, null, parameters, returnType);
}
asLua() {
return this.exportLuaFuncType();
}
asTs() {
return this.exportTsFuncType();
}
}
export class InterfaceExportable implements ExportableType {
constructor(private intf: { key: string; valueType: ExportableType }[]) {}
asLua() {
return `{ ${this.intf
.map(({ key, valueType }) => `${key}: ${valueType.asLua()}`)
.join(", ")} }`;
}
asTs() {
return `{ ${this.intf
.map(({ key, valueType }) => `${key}: ${valueType.asTs()}`)
.join(", ")} }`;
}
}
export class RecordExportable implements ExportableType {
constructor(
private keyType: ExportableType,
private valueType: ExportableType
) {}
asLua() {
return `table<${this.keyType.asLua()}, ${this.valueType.asLua()}>`;
}
asTs() {
return `Record<${this.keyType.asTs()}, ${this.valueType.asTs()}>`;
}
}
export class UnionExportable implements ExportableType {
constructor(private types: ExportableType[]) {}
asLua() {
return this.types.map((t) => t.asLua()).join("|");
}
asTs() {
return this.types.map((t) => t.asTs()).join(" | ");
}
}
export const stringExportable = new LiteralExportable("string");
export const numberExportable = new LiteralExportable("number");
export const booleanExportable = new LiteralExportable("boolean");
export const anyExportable = new LiteralExportable("any");
export const nullExportable = new IndependentLiteralExportable({
lua: "nil",
ts: "null",
});
export const integerExportable = new IndependentLiteralExportable({
lua: "integer",
ts: "tfm.integer",
});
export const tableExportable = new IndependentLiteralExportable({
lua: "table",
ts: "object",
});