简体   繁体   English

在 TypeScript 中定义和扩展自定义类型

[英]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

See playground 看游乐场


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

See playground 看游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在 Typescript 中扩展类型吗? - Possible to extend types in Typescript? 在TypeScript中扩展基本类型,错误:“_这未定义...” - Extend Basic Types in TypeScript, Error: “_this is not defined…” 使用 TypeScript checkJs 在 JSDoc 中扩展泛型类型的方法? - Way to extend generic types in JSDoc with TypeScript checkJs? TypeScript:定义类型的正确方法是什么 - TypeScript : What is the right way to define types TypeScript:使用自定义验证值扩展键 - TypeScript: extend keys with custom validation values 因果类型脚本接口XXX不能同时扩展类型&#39;YYY - Karma-typescript Interface XXX cannot simultaneously extend types ' YYY TypeScript:是否可以定义一个接受不同类型 arguments 的可变参数 function? - TypeScript: Is it possible to define a variadic function that accepts different types of arguments? 你如何定义在 Typescript 中采用两个 object 类型的函数? - How do you define functions that take two object types in Typescript? Typescript:将类型定义为具有部分存在的 object 的类型的联合 - Typescript: Define type as union of types with a partially present object 如何像流一样在 Typescript 中定义条件字符串类型? - How to define conditional string types in Typescript like flow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM