我有从 prismabox 生成的模式,当我使用该模式并构建 d.ts 时。所有类型都重复写入,而不是使用@import,并在大型应用程序中导致错误
The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed
。
如何解决这个问题?
Person.ts
import { Type } from "@sinclair/typebox";
export const Person = Type.Object({
id: Type.String({ additionalProperties: false }),
name: Type.String({ additionalProperties: false }),
statusId: Type.String({ additionalProperties: false }),
...
});
export const PersonInputSelect = Type.Object({
id: Type.String({ additionalProperties: false }),
name: Type.String({ additionalProperties: false }),
statusId: Type.String({ additionalProperties: false }),
});
index.ts
import { Type } from "@sinclair/typebox";
import {
Person,
PersonInputSelect,
PersonInputInclude,
PersonInputOrderBy,
PersonWhere,
} from "./Person";
export const schemas = {
person: {
name: "person",
model: Person,
select: PersonInputSelect,
include: PersonInputInclude,
where: PersonWhere,
orderBy: PersonInputOrderBy,
},
};
tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"disableSizeLimit": true,
"preserveSymlinks": true,
"moduleResolution": "Node",
"declaration": true,
"declarationDir": "./build",
"skipLibCheck": true,
"emitDeclarationOnly": true,
"noEmit": null
},
"include": ["src/index.ts"]
}
生成的文件太大且重复
person.d.ts
export declare const PersonWhere: import("@sinclair/typebox").TObject<{
id: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
statusId: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
blacklist: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
positionId: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
departmentId:
...
}>;
export declare const PersonInputSelect: import("@sinclair/typebox").TObject<{
id: import("@sinclair/typebox").TString;
name: import("@sinclair/typebox").TString;
statusId: import("@sinclair/typebox").TString;
}>;
index.d.t.s
export declare const schemas: {
person: {
name: string;
model: import("@sinclair/typebox").TObject<{
id: import("@sinclair/typebox").TString;
name: import("@sinclair/typebox").TString;
...
orderBy: import("@sinclair/typebox").TObject<{
id: import("@sinclair/typebox").TString;
departmentId: import("@sinclair/typebox").TString;
groupId: import("@sinclair/typebox").TString;
statusId: import("@sinclair/typebox").TString;
}>;
};
};
如何让 tsc 使用 @import 编译 typescript 文件并且不重复编写类型。
预期结果
import { Person } from "./person"
export declare const schemas: {
person: {
name: string;
model: import("./person").Person;
select: import("./person").InputSelect;
.....
};
};
....
我找到了解决方案。我们需要像这样自己写定义类型
Person.ts
import { TObject, TString, Type } from "@sinclair/typebox";
export type TPerson = TObject<{
id: TString;
name: TString;
statusId: TString;
blacklist: TString;
positionId: TString;
departmentId: TString;
groupId: TString;
subGroupId: TString;
areaId: TString;
subAreaId: TString;
barcodeId: TString;
firstName: TString;
lastName: TString;
}>;
export const Person: TPerson = Type.Object({
id: Type.String({ additionalProperties: false }),
name: Type.String({ additionalProperties: false }),
statusId: Type.String({ additionalProperties: false }),
blacklist: Type.String({ additionalProperties: false }),
positionId: Type.String({ additionalProperties: false }),
departmentId: Type.String({ additionalProperties: false }),
groupId: Type.String({ additionalProperties: false }),
subGroupId: Type.String({ additionalProperties: false }),
areaId: Type.String({ additionalProperties: false }),
subAreaId: Type.String({ additionalProperties: false }),
barcodeId: Type.String({ additionalProperties: false }),
firstName: Type.String({ additionalProperties: false }),
lastName: Type.String({ additionalProperties: false }),
});
import { Type } from "@sinclair/typebox";
import { Person } from "./Person";
index.ts
export const schemas = {
person: {
name: "person",
model: Person,
},
};
export const typeSchema = Type.Object({
person: Type.Object({
name: Type.Literal("person"),
model: Person,
}),
});
结果
import { TObject, TString } from "@sinclair/typebox";
export type TPerson = TObject<{
id: TString;
name: TString;
statusId: TString;
blacklist: TString;
positionId: TString;
departmentId: TString;
groupId: TString;
subGroupId: TString;
areaId: TString;
subAreaId: TString;
barcodeId: TString;
firstName: TString;
lastName: TString;
}>;
export declare const Person: TPerson;
export declare const schemas: {
person: {
name: string;
model: import("./Person").TPerson;
};
};
export declare const typeSchema: import("@sinclair/typebox").TObject<{
person: import("@sinclair/typebox").TObject<{
name: import("@sinclair/typebox").TLiteral<"person">;
model: import("./Person").TPerson;
}>;
}>;