@cats-oss/ts-proto-optimize
[警告] 它是用于优化TypeScript protobuf类型定义文件的CLI工具.
优化仅用于获得类型安全的原生取出物的定义. 使用 Babel 汇编成为可能.
特征
- 全部提取
enum。 。 。 .- 从命名空间和集合名称中删除 。 (用帕斯卡语)
- 替换
interface与enum。 。 。 .
- 删除
import * as $protobuf from "protobufjs";。 。 。 .- 保持小的捆绑大小.
- 删除全部
Long,无能,和可选. - 删除全部
class。 。 。 . - 删除所有函数声明类型的别名.
- 添加
export关键词到interface内namespace。 。 。 .
安装
$ npm i -D @cats-oss/ts-proto-optimize
使用量
# Output to stdout
$ $(npm bin)/ts-proto-optimize path/to/proto.d.ts
# Output to file
$ $(npm bin)/ts-proto-optimize path/to/proto.d.ts --output dist/to/proto.ts
示例
输入 :
import * as $protobuf from 'protobufjs';
/** Namespace root. */
export namespace root {
/** Namespace api. */
namespace api {
/** Properties of a Foo */
interface IFoo {
/** Foo str */
str?: string | null;
/** Foo number */
num?: number | Long | null;
}
/** class Enumerate */
class Foo implements IFoo {
/**
* comment
*/
constructor(properties?: root.api.IFoo);
/** comment */
public str: string;
/** comment */
public num: number;
}
/** Enum enum */
enum Enum {
KEY1_SNAKE_CASE = 0,
KEY2_SNAKE_CASE = 1,
KEY3_SNAKE_CASE = 2,
}
namespace Nest {
/**
* Callback as used by {@link ...}.
* @param error Error, if any
* @param [response] GetFooResponse
*/
type GetFooCallback = (
error: Error | null,
response?: root.api.GetFooResponse,
) => void;
/** Enum enum. */
enum Enum {
KEY1 = 0,
KEY2 = 1,
KEY3 = 2,
}
/** Properties Data */
interface IData {
name?: string | null;
value?: number | null;
field?: root.api.Nest.IData | null;
enumratee?: root.api.Nest.Enum | null;
}
/** Represents Data */
class Data implements IData {
constructor(properties?: root.api.Nest.IData);
}
}
}
}
输出 :
/** Namespace root. */
export declare namespace root {
/** Namespace api. */
export namespace api {
/** Properties of a Foo */
export interface IFoo {
/** Foo str */
str: string;
/** Foo number */
num: number;
}
export namespace Nest {
/** Properties Data */
export interface IData {
name: string;
value: number;
field: root.api.Nest.IData;
enumratee: RootApiNestEnum;
}
}
}
}
export enum RootApiEnum {
KEY1_SNAKE_CASE = 0,
KEY2_SNAKE_CASE = 1,
KEY3_SNAKE_CASE = 2,
}
export enum RootApiNestEnum {
KEY1 = 0,
KEY2 = 1,
KEY3 = 2,
}