努力理解打字稿的减少

发布于 2025-02-12 02:12:34 字数 1224 浏览 2 评论 0原文

假设我将实用程序类型定义为具有所有属性作为字符串的对象

type Stringify<T> = {
  [P in keyof T]: string;
};

,我想定义一个将对象的任何属性转换为字符串的函数,例如

function stringify<T>(obj: T): Stringify<T> {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        acc[k] = `${v}`;
        return acc;
    }, {});
}

我不知道如何定义在 我一直在阅读有关铸造,类型防护和功能过载的每个cicle,

.reduce<Partial<T>>((acc, [k, v]) => /* ... */) // type Partial<T> is not assignable to type Stringify<T>
.reduce((acc: Partial<T>, [k, v]) => /* ... */) // type Partial<T> is not assignable to type Stringify<T>
.reduce(/* ... */, {} as Partial<T>) // type Partial<T> is not assignable to type Stringify<T>

.reduce(/* ...*/
  acc[k] = `${v}`; // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Stringify<T>'. No index signature with a parameter of type 'string' was found on type 'Stringify<T>'
}, {} as Stringify<T>)

但我仍然无法为这样一个简单的示例提供解决方案。

Let's say I define a Utility type to have an object with all attributes as string

type Stringify<T> = {
  [P in keyof T]: string;
};

and I want to define a function that converts any attribute of an object to string such as

function stringify<T>(obj: T): Stringify<T> {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        acc[k] = `${v}`;
        return acc;
    }, {});
}

I can't figure out how to define the accumulator that is being fulfilled at every cicle in the reduce callback

.reduce<Partial<T>>((acc, [k, v]) => /* ... */) // type Partial<T> is not assignable to type Stringify<T>
.reduce((acc: Partial<T>, [k, v]) => /* ... */) // type Partial<T> is not assignable to type Stringify<T>
.reduce(/* ... */, {} as Partial<T>) // type Partial<T> is not assignable to type Stringify<T>

.reduce(/* ...*/
  acc[k] = `${v}`; // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Stringify<T>'. No index signature with a parameter of type 'string' was found on type 'Stringify<T>'
}, {} as Stringify<T>)

I've been reading about casting, type guards, and function overload but I still can't arrive to a solution to such a simple example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-02-19 02:12:34

您的stringify generic基本上与实用程序类型record进行相同的作业。

您可以使用通用参数充当Record&lt;字符串,任何&gt;的速记,以节省一些详细的杂句。

function stringify<T = Record<string, any>>(obj: T): T {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        return { ...acc, [k] : `${v}` }
    }, {} as T);
}

更新:键入返回值作为record&lt; string,string&gt;

function stringify<T = Record<string, any>, R = Record<string, string>>(obj: T): R {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        return { ...acc, [k] : `${v}` }
    }, {} as R);
}

const a = stringify({ a: 1, b: 2})

console.log(a)

Your Stringify generic is essentially doing the same job as the utility type Record.

You can use the generic argument to act as a shorthand for Record<string, any> to save a bit of verbosity.

function stringify<T = Record<string, any>>(obj: T): T {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        return { ...acc, [k] : `${v}` }
    }, {} as T);
}

Update: typing return value as Record<string, string>

function stringify<T = Record<string, any>, R = Record<string, string>>(obj: T): R {
    return Object.entries(obj).reduce((acc, [k, v]) => {
        return { ...acc, [k] : `${v}` }
    }, {} as R);
}

const a = stringify({ a: 1, b: 2})

console.log(a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文