gpt4 book ai didi

javascript - 您可以将带有装饰器的 Typescript 类导出为普通类(没有装饰器)吗?

转载 作者:行者123 更新时间:2023-12-04 01:25:51 24 4
gpt4 key购买 nike

假设我有这样的类(class):

import { Injectable } from '@nestjs/common';
import { IsString } from 'class-validator';
import { prop } from '@typegoose/typegoose';

@Injectable()
export class Human
{
@prop({ unique: true })
@IsString()
readonly name: string;
}

问题是:我可以导出这个类,以便接收端在没有装饰器的情况下得到它,如下所示:
class Human
{
readonly name: string;
}

我想导出该类(它具有装饰器的其他依赖项),以便导入它的文件不需要用于这些装饰器的依赖项,例如nestjs、class-validator 和typegoose。

我试图不写出“只读名称:字符串;”不止一次。

最佳答案

所以我想通了,部分归功于 zett42。

您可以定义一个 type在 typescript 中,所以如果你只是创建一个等于类的类型,然后导出该类型,你将有效地导出没有装饰器的类定义。您也可以使用 interfaces ,但类型似乎更干净。但是我不确定类型的行为是否与接口(interface)相同,是时候阅读 Typescript 文档了!

有类型:

export type THuman = Human;

带接口(interface):
export interface IHuman extends Human {}

关于javascript - 您可以将带有装饰器的 Typescript 类导出为普通类(没有装饰器)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com