惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
H
Heimdal Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Secure Thoughts
博客园 - Franky
A
Arctic Wolf
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
The Last Watchdog
The Last Watchdog
博客园_首页
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
T
Threatpost
Y
Y Combinator Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
博客园 - 叶小钗
量子位
Security Latest
Security Latest
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 聂微东
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
P
Privacy International News Feed
Help Net Security
Help Net Security
博客园 - 司徒正美
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com

TypeScript

ts 生态有没有开源的简单的 kv 配置管理项目 - V2EX 你们是怎么学习 typescript 的? - V2EX 用 Go 移植的 TypeScript 7 要来了 - V2EX TypeScript5.9,仿佛走出草原来到了现代社会 - V2EX Typescript 有没有能对接 AI 的知识库或者文档?能处理稍微复杂点的类型 - V2EX typescript 如何返回一个动态对象类型 - V2EX ts 有没有直接能查看最终类型的方法 - V2EX TypeScript Native 预览版发布了 - V2EX 你们 typescript 用自带枚举还是字符串枚举,感觉字符串枚举类型用起来有点不可靠啊 - V2EX effect-ts - V2EX typescript 达人乱入 - V2EX TypeScript 宣布原生移植计划,性能将提升 10 倍? - V2EX #笔记#更好的 ts 重载声明方法 - V2EX 使用 satisfies 实现穷举类型保护 - V2EX 请教一个 TS 问题,不知能否实现 - V2EX 装饰器能不能装饰类的所有方法 - V2EX TypeSciprt 类型推断,如何进一步收窄类型? - V2EX TypeScript 项目打包发布到 npm,如何给方法和接口添加 dts TypeScript 类型推导真强大。 - V2EX 为啥下面这段代码 TS 不报错 - V2EX ts 类型约束+请求参数校验+自动生成 openapi/swagger 文档,这三者目前有整合为一体的最佳实践吗 - V2EX TypeScript 能不能别这么古怪,这行为什么会报错呢? - V2EX 怎么每次碰到想不通的代码行都出在 TypeScript😢 这行到底哪里有问题? - V2EX 低估了 typescript 的难度 - V2EX 前端写 nestjs 后端,用 TypeORM 时感到非常困惑,不知道是不是我的理解有问题 - V2EX 我列举了市面上最好的几个 TypeScript 教程, 想深入学习 TypeScript 的同学以后不用走弯路了 - V2EX 这样的函数类型定义在 ts 里面叫什么? - V2EX typescript 如何忽略泛型参数啊? - V2EX TypeScript 现在的应用场景 - V2EX ==TypeScript 写的 npm 包可以用 javascript 使用包的函数吗 - V2EX react 如何优雅的实现批量删除+二次确认 - V2EX TypeScript 怎么 wrap 一个函数并修改一下返回值? - V2EX 想请教个 TypeScript 抽象类继承父类的问题。 - V2EX vscode 纯 js 项目有类型推断和成员变量提示,但是如果 ts 混用的话, js 导入就全变成 any 了,请问有办法解决吗 这个项目这样定义 Typescript 类型和使用,学到了,有几个问题? - V2EX 请教一个 Typescript 的问题 - V2EX 这种的类型是不是实现不了(TypeScript) - V2EX 问题一个 typescript 类型包裹问题 - V2EX 遇到了极其诡异的不符合进入 if 条件的要求但是还是进入了 - V2EX typescript 的类型处理 - V2EX 想问问大家 TypeScript 中关于 null 处理的方式 - V2EX 有没有声明 typescript 类型的好办法以及前端样式的健壮性问题 - V2EX typescript 类型断言问题 - V2EX TypeScript 中类型编程简介 - V2EX [求助] 类型 { K:V } --> { "type": K , "conf": V } 且约束 - V2EX 大佬们, vscode 开发 ts,这个地方的提示,怎么修改成中文啊 - V2EX TypeScript 中「类型定义的命名空间」使用 - V2EX 有没有办法把 ts 的 error 报错改成 warning - V2EX [求助] 函数中使用泛型缩小参数类型 - V2EX
关于 TypeScript 装饰器和接口的问题,请教一下各位 - V2EX
DreamingCTW · 2025-04-03 · via TypeScript

我是做 Java 后端的,这两天正在系统化的学习 ts ,学到装饰器的时候,有点疑惑,希望可以指导下,谢谢大家。以下是 Demo

type Constructor = new (...args: any[]) => {};

function LogTime<T extends Constructor>(target: T) {
  return class extends target {
    createTime: Date;
    constructor(...args: any[]) {
      super(...args);
      this.createTime = new Date();
    }
    getCreateTime() {
      return `该对象的创建时间是:${this.createTime}`;
    }
  };
}

@LogTime
class Person {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

interface Person {
  getCreateTime(): void;
}

const person = new Person("小明", 18);
console.log(person.getCreateTime());

如果不声明 interface Person { getCreateTime(): void },那么 person.getCreateTime()会报错,提示 Person 对象中没有这个属性。加上这个接口就不会报错了,但有一个问题,接口中规范了 getCreateTime()函数的返回值为 void ,实际上装饰器中 getCreateTime()返回的是 string ,这不矛盾吗?我尝试修改接口中的 getCreateTime()函数的返回值,发现改成任意类型都可以...

现在给我的感觉就是,为了让 person.getCreateTime()代码不报错,就打了 interface Person 这个补丁,而且可以不强制遵循这个补丁的规范。

由此我想到了另一个问题,从上述代码看,Person 既是一个 class ,也是一个 interface ,我还可以创建一个新的 class 来 implements Person ,发现 class Person 中的所有属性也都要实现,所以 interface Person 是把 class Person 转成了接口?

class NewPerson implements Person {
  name: string;
  age: number;
  createTime: Date;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
    this.createTime = new Date();
  }
  getCreateTime() {
    return `该对象的创建时间是:${this.createTime}`;
  }
}