[英](TypeScript) Type Alias / Interface defined by object literal
I am making some schema of JSON message to transfer between server & client, for serialization I created an object literal as a template such as below我正在制作一些 JSON 消息模式以在服务器和客户端之间传输,为了进行序列化,我创建了一个对象文字作为模板,如下所示
type uint8 = number;
const schema1 = {
type: 'object',
fields: {
type: { type: 'uint8' },
title: { type: 'string', optional: true }
}
};
// can below automatically defined by schema1?
type schema1Type = {
type: number,
title?: string
};
const data1: schema1Type = {
type: 1,
title: 'Hello World'
};
First of all, you're going to need to the type for your schema definitions.首先,您将需要模式定义的类型。 I'm thinking something like this:
我在想这样的事情:
interface SchemaOptional { optional?: boolean }
interface SchemaNumber extends SchemaOptional { type: 'uint8' | 'int8' | 'float' }
interface SchemaString extends SchemaOptional { type: 'string' }
interface SchemaObject extends SchemaOptional {
type: 'object',
fields: Record<string, SchemaEntry>
}
type SchemaEntry = SchemaObject | SchemaNumber | SchemaString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.