[英]Define and extend custom types in TypeScript
I am trying to define custom types in TypeScript with some additional extension methods, also want to allow other uses of my library to extend this type as well.我正在尝试使用一些额外的扩展方法在 TypeScript 中定义自定义类型,也希望允许我的库的其他用途也扩展这种类型。
Want to implement the toJson method and more utility functions, I tried to do that by creating two files: domain.d.ts with this code:想要实现 toJson 方法和更多实用功能,我尝试通过创建两个文件来做到这一点: domain.d.ts 使用以下代码:
export type TermOperator = '=' | '>' | '<' | '!=' | '<=' | '>=' | 'in' | 'not in' | 'like';
export type DomainOperator = '&' | '|' | '!';
export type Term = [left: string, operator: TermOperator, right: string | number | Date | EnumType | undefined | boolean];
export type ScalarDomain = [...Array<DomainOperator | Term>];
export interface Domain extends Array<ScalarDomain | Domain | DomainOperator | Term>
{
toJson(): string;
flat(): [string | number | Date | EnumType | boolean | undefined];
parse(): void;
}
and tried to add new file domain.extension.ts with the following code:并尝试使用以下代码添加新文件 domain.extension.ts :
Domain.prototype.toJson = function (){ return 'json object'; }
But I am getting errors such as:但我收到错误,例如:
Error TS2693 (TS) 'Domain' only refers to a type, but is being used as a value here.错误 TS2693 (TS) 'Domain' 仅指一种类型,但在此处用作值。
don't know exactly how to achieve this, as I am a beginner in JavaScript.不知道如何实现这一点,因为我是 JavaScript 的初学者。
For more context, here is how I am planning to use this code:有关更多上下文,这是我打算如何使用此代码的方式:
const term1: Term = ['name', '=', 'amine'];
const term2: Term = ['age', '>', 25];
const term3: Term = ['address', 'like', 'eloued'];
const domain1: Domain = ['|', term1, term2, '&', term3];
const domain2: Domain = ['&', term1, term2, '&', ['city', '=', 'Malaga']];
const domain3: Domain = [domain1, domain2];
const jsonStr: string = domain3.toJson();
A type cannot add functionality.类型不能添加功能。
All that this does:所有这一切:
const domain1: Domain = ['|', term1, term2, '&', term3];
is tell Typescript that domain1
is expected to be of type Domain
, and if it's not then a type error will be shown.告诉 Typescript domain1
应该是Domain
类型,如果不是,则会显示类型错误。 It does not, and cannot , change the features on the array that is being assigned to this variable.它不会也不能更改分配给此变量的数组上的特征。
Imagine all type annotations were removed.想象一下,所有类型注释都被删除了。 Would your code work?你的代码能用吗? Because that's exactly what happens when your code executes.因为这正是你的代码执行时发生的事情。
If you want to provide additional methods to this array, you would typically wrap that value in a class that allows you declare those other methods.如果您想为此数组提供其他方法,您通常会将该值包装在 class 中,以允许您声明其他方法。
For example:例如:
export class Domain {
constructor(public terms: ScalarDomain) {}
toJson() {
return JSON.stringify(this.terms)
}
flat() {
return this.terms.flat()
}
parse() {
console.log('implete parse here')
}
}
Which you would use like:您会使用如下:
const term1: Term = ['name', '=', 'amine'];
const term2: Term = ['age', '>', 25];
const term3: Term = ['address', 'like', 'eloued'];
const domain1 = new Domain(['|', term1, term2, '&', term3]);
domain1.toJson() // fine
Or a more functional approach would be to create functions that accept your data and do things with them.或者更实用的方法是创建接受您的数据并用它们做事的函数。
function domainToJson(domain: Domain) { return JSON.stringify(domain) }
function domainFlat(domain: Domain) { return domain.flat() }
const term1: Term = ['name', '=', 'amine'];
const term2: Term = ['age', '>', 25];
const term3: Term = ['address', 'like', 'eloued'];
const domain1: Domain = ['|', term1, term2, '&', term3]
domainToJson(domain1) // fine
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.