如何将任何类型的值转换为字符串值(Typescript)?

huangapple go评论82阅读模式
英文:

How to convert object of any type values, to object of string values (Typescript)?

问题

  1. 我有一个对象
  2. /*
  3. type A = {
  4. property1: any;
  5. property2: any;
  6. }
  7. */

我知道对象中的值都是字符串,但我不想强制类型转换。

  1. // 我不想这样做
  2. const obj2 = obj1 as Record<keyof typeof obj1, string>

相反,我想以正确的方式推断它,使用typescript predicates。这是我的尝试。

  1. function getIsCorrectType<T extends Record<string, any>>(
  2. obj: T
  3. ): obj is Record<keyof T, string>{
  4. return true; // 假设我手动检查了每个值是否为字符串
  5. }

然而,我现在遇到了一个错误

  1. A type predicatetype必须可分配给其parametertype
  2. Type 'Record<keyof T, string>' is not assignable to type 'T'.
  3. 'Record<keyof T, string>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Record<string, any>'.
  4. Type 'string' is not assignable to type 'T[P]'.

对我来说这听起来很奇怪。我应该能够将string分配给T[P] = any,对吗?我做错了什么?是否有其他解决方案?

  1. <details>
  2. <summary>英文:</summary>
  3. I have an object.

let obj1: A;
/*
type A = {
property1: any;
property2: any;
}
*/

  1. I know that the values in the object are all strings, but I **don&#39;t** want to forcefully typecast.

// I don't want to do this
const obj2 = obj1 as Record<keyof typeof obj1, string>

  1. Instead, I want to infer it in the right way, using **typescript predicates**. This is my attempt to do it.

function getIsCorrectType<T extends Record<string, any>>(
obj: T
): obj is Record<keyof T, string>{
return true; // assume that I manually checked each value to be a string
}

  1. However I now get an error

A type predicate's type must be assignable to its parameter's type.
Type 'Record<keyof T, string>' is not assignable to type 'T'.
'Record<keyof T, string>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Record<string, any>'.
Type 'string' is not assignable to type 'T[P]'.

  1. To me this sounds crazy. I should be able to assign `string` to `T[P] = any`, right? What am I doing wrong? Is there any alternative solution to this?
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. ```typescript
  6. 为了使其工作,您只需要推断一组键,而不是整个对象:
  7. const isString = (value: unknown): value is string => typeof value === 'string'
  8. const getIsCorrectType = <K extends string>(
  9. obj: Record<K, unknown>
  10. ): obj is Record<K, string> =>
  11. Object.values(obj).every(isString)
  12. const x = getIsCorrectType({ 1: 'sdf' })
  13. K - 用于键的推断
  14. 还添加了isString自定义类型保护
英文:

In order to make it work, you need to infer just a set of keys instead of whole object:

  1. const isString = (value: unknown): value is string =&gt; typeof value === &#39;string&#39;
  2. const getIsCorrectType = &lt;K extends string&gt;(
  3. obj: Record&lt;K, unknown&gt;
  4. ): obj is Record&lt;K, string&gt; =&gt;
  5. Object.values(obj).every(isString)
  6. const x = getIsCorrectType({ 1: &#39;sdf&#39; })

K - is used for keys inference

Also I have added isString custom typeguard

huangapple
  • 本文由 发表于 2023年1月9日 14:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定