English 中文(简体)
了解对象类型
原标题:Understanding object types
  • 时间:2024-07-23 12:50:41
  •  标签:
  • typescript
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 35 mins ago. Improve this question The following cases confuse me - was hoping someone could explain them. given the following types and variables (playground link) type StringKeyedObject = Record type NumberKeyedObject = Record const s = Symbol() let sko: StringKeyedObject = { a: a } let nko: NumberKeyedObject = { 1: a } Why is sko[1] = a valid while nko[ a ] = a throws index expression is not of type number (playground link) Why is sko = { [s]: a } valid while sko[s] = a throws Type unique symbol cannot be used as an index type (playground link) As seen in case 1 below, why does a Record extend Record ? In order for me to get the behavior I want, I need to infer K as seen in case 2, but I don t understand why that s necessary. (playground link) // case 1 type TExtendsStringKeys1 = T extends Record ? has string keys : never // type X = has string keys type X = TExtendsStringKeys1 // case 2 type TExtendsStringKeys2 = T extends Record ? K extends string ? has string keys : never : never // type Y = never type Y = TExtendsStringKeys2
问题回答

暂无回答




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签