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

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

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 你们 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 遇到了极其诡异的不符合进入 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
wednesdayco · 2024-01-30 · via TypeScript

这是一个创建于 866 天前的主题,其中的信息可能已经有所发展或是发生改变。

需求: 一个类型必须包含 id 属性,值的类型为 string ,同时它可能包含其他的属性,但值的类型必须是 number 。

第 1 条附言  ·  2024 年 1 月 30 日

其实只提示类型的话,大家给出了答案。问题出在初始化上:

需求场景也很简单,我有一个列表,这个列表每一行有一个 id ,是 string 的。
但是其他的属性都是 number 。
const data: Item = {
id: 'xxx-xxx-xxx', // id 只能是 string
cid: 123123123,
name: 233233,
...//任意属性,但值必须是 number 不能是其他的或者 string
}

第 2 条附言  ·  2024 年 1 月 30 日

折中玩法

const id = Symbol('id');
type CustomType = {
[t: string]: number
[id: symbol]: string
}

const data:CustomType = {
[id]: '123123xxx123',
v2: 123233
}

nomagick

1

nomagick      2024 年 1 月 30 日   ❤️ 1

```typescript
interface wtf {
id: string;
[k: string]: number;
}

```

liahu

2

liahu      2024 年 1 月 30 日

{
id: string
[key: string]: any
}
???

simoger

3

simoger      2024 年 1 月 30 日

interface MyObject {
id: string;
[key: string]: number | string;
}
这种?

lilei2023

4

lilei2023      2024 年 1 月 30 日

这不是有三位热心网友帮你实现了一个么?

cvooc

5

cvooc      2024 年 1 月 30 日

这种?

```ts
interface Obj {
id: string
}
interface Somebody extends Obj {
key?: number
}
```

cvooc

7

cvooc      2024 年 1 月 30 日

明白你意思了, 我理解问题, 1 楼是对的

cvooc

8

cvooc      2024 年 1 月 30 日

你不妨 给个具体示例, 看你回复上面都不对完全不理解什么意思了

wednesdayco

9

wednesdayco      2024 年 1 月 30 日

@retrocode 1 楼也不对
Property 'id' of type 'string' is not assignable to 'string' index type 'number'.ts(2411)

wednesdayco

10

wednesdayco      2024 年 1 月 30 日

@liahu 其他的属性值的类型必须是 number ,除了属性 id 的值的类型必须是 string

Bologna

12

Bologna      2024 年 1 月 30 日

放一个数据样例更好解决问题

tearzx

13

tearzx      2024 年 1 月 30 日

interface A{
[key: string]: number;
}

interface B {
id: string;
}

type C = A | B;

nomagick

14

nomagick      2024 年 1 月 30 日

还真是,等一个答案

wednesdayco

16

wednesdayco      2024 年 1 月 30 日

@MRG0

const data: IData = {
id: 'xxx-xxx-xxx', // id 只能是 string
cid: 123123123,
name: 233233,
...//任意属性,但值必须是 number 不能是其他的或者 string
}

Pencillll

17

Pencillll      2024 年 1 月 30 日 via Android

实现不了,因为没有办法把 'id' 从 string 里面排除出来

fannheyward

18

fannheyward      2024 年 1 月 30 日

#3 是对的

```
interface IData {
id: string
[k: string]: number | string
}
```

rrfeng

20

rrfeng      2024 年 1 月 30 日

string indexer MUST accommodate all members

nomagick

21

nomagick      2024 年 1 月 30 日   ❤️ 1

我解开了

interface NumO {
[k: string]: number;
}

type WithID<T> = {
id: string;
} & T;
type WTF = WithID<NumO>;

const v = {
id: 'wtf'
} as WTF;

// OK
v.id = 'str';
v.a = 3;
v.b = 4;
// Not OK
v.c = 'sr';

ntdll

22

ntdll      2024 年 1 月 30 日   ❤️ 1

interface A {
id: string;
}

interface B {
[k: string]: number
}

type AB = A & B

function sample(arg: AB) {
arg.id = 'aaa'
arg.bcd = 1234
}

nomagick

24

nomagick      2024 年 1 月 30 日

再加一个类型参数,现场合并。。。
真拧巴。。。

rrfeng

25

rrfeng      2024 年 1 月 30 日   ❤️ 1

@gam2046 这样实际上 AB 不存在……,所以是 bug 。

NoManPlay

26

NoManPlay      2024 年 1 月 30 日   ❤️ 1

```
type CustomType = {
[key: string]: number | string;
} & {
id: string;
};

// 通过映射类型和条件类型来移除 id 属性外的 string 类型
type StrictCustomType = {
[K in keyof CustomType]: CustomType[K] extends string ? (K extends 'id' ? string : never) : number;
};
```

Pencillll

28

Pencillll      2024 年 1 月 30 日

对于初始化来说,如果可以加个 helper 函数的话,倒是有一种办法:

type CoercedItem<T> = {
[K in keyof T]: K extends 'id' ? string : number
}

type ValidItem<T> = T extends CoercedItem<T> ? 'id' extends keyof T ? T : never : never

function makeItem<T>(item: ValidItem<T>): T {
return item
}

const a = makeItem({ id: 'x', no: 1 })

a.id = 'y'
a.no = 2

const b = makeItem({ id: 'z' })

// 报错
const c = makeItem({ id: 1, no: 1 })

// 报错
const d = makeItem({ no: 1 })

Pencillll

29

Pencillll      2024 年 1 月 30 日

不过上面这样有个缺陷是初始化之后的对象不能再添加新的属性

CLMan

30

CLMan      2024 年 1 月 30 日

好奇什么场景需要这么复杂的类型定义呢?

或者说类似问题的类型约束的严谨性和代码简洁应该如何取舍?

我个人是把 TS 当作一个不太完美的类型约束工具,更倾向于代码简洁性。

Gaoti

31

Gaoti      2024 年 2 月 1 日

```ts
type Others = Record<string, number>;

type T<O = Others> = {
id: string;
} & {
[K in keyof O]: K extends "id" ? string : number;
};

const obj: T<{ id: string; aaa: number }> = {
id: "1",
aaa: 123,
bbb: 1, // error
};
```

使用的时候泛型传入对应的类型就行,但是和 #28 说的一样,泛型中没有定义的的字段会报错

chnwillliu

32

chnwillliu      2024 年 3 月 13 日 via Android

const id = ()=> ('i' + 'd ').trim();

const n:number = obj[id()];

如果有这样的类型你这样操作那 TS 该不该报错? key 是运行时决定,所以有可能是 string 也有可能是 number 。