InfluxDB提供了一个不错的TypeScript接口。可以使用ISchemaOptions提供Record-Schema。缩小与该架构有关的IPoint接口会很好。使用条件和映射类型,但无法使其正常工作。这有可能吗?
import {FieldType, IPoint, ISchemaOptions} from "influx";
const schema:ISchemaOptions = {
measurement: "resources",
fields: {
cpu: FieldType.FLOAT,
},
tags: ["test"]
};
type Field<Type extends FieldType> =
Type extends FieldType.BOOLEAN ? boolean:
Type extends FieldType.FLOAT ? number:
Type extends FieldType.INTEGER ? number:
Type extends FieldType.STRING ? string:
never;
type Fields<S extends ISchemaOptions> = {
[F in keyof S['fields']]: Field<S['fields'][F]>
}
type DataPoint<S extends ISchemaOptions> = {fields: Fields<S>} & IPoint;
function data<S extends ISchemaOptions>(s:S, fields:Fields<S>) :DataPoint<S> {
const d = {} as any;
d.fields = fields;
return d as DataPoint<S>
}
data(schema, {cpu:'string'});
没有收到错误消息,但是应该显示错误,因为属性“ cpu”应为数字类型而不是字符串。
TypeScript游乐场:this way