这段代码按预期工作,但它是一个类型错误富矿。是否真的可以有效地键入检查此代码,或者我应该放弃并
@ts-expect-error
一切?
我确实安装了
@types/ramda
,但它对此根本不满意,即使我确实有这些类型可用,我仍然发现,一般来说,尝试有效地输入 ramda 确实是非常毛茸茸的(否则我是一个大图书馆的粉丝,感谢所有维护者的努力)。
我知道一般来说输入
pipe
存在问题(它可能需要更高种类的类型),但除此之外,我正在努力满足这里的检查器。
这是一个失败的原因吗?
import { camel, isArray, isObject } from 'radash';
import { T, cond, fromPairs, head, identity, juxt, last, map, o, pipe, toPairs, useWith } from 'ramda';
const camelArray: <A>(arr: A[]) => A[] = map((arr) => camelKeys(arr));
// No overload matches this call.
// The last overload gave the following error.
// Argument of type '(fn: (x: unknown) => unknown, list: readonly unknown[]) => unknown' is not assignable to parameter of type '(args_0: (x: unknown) => unknown) => unknown'.
// Target signature provides too few arguments. Expected 2 or more, but got 1.
const mapKeysAndValues = useWith(pipe(map, fromPairs), [identity, toPairs]);
// Expected 7 arguments, but got 1.
const camelObj = mapKeysAndValues(
juxt([
o(camel, head),
o((x) => camelKeys(x), last),
]),
);
// No overload matches this call.
// The last overload gave the following error.
// Type 'unknown' is not assignable to type '(value: any) => unknown'.
const camelKeys = cond([
[isArray, camelArray],
[isObject, camelObj], // The above error is actually emitted at camelOj here.
[T, identity],
]);
export { camelKeys };
不确定您想要对此进行多少ramdify,很抱歉,如果我的回答对这个问题的具体问题没有帮助,请提供指向打字稿游乐场的链接,以防您需要具体帮助。
这最终有助于将对象属性驼峰化, 因为这似乎是您的预期结果。请提供
given
与 expected
,以防情况并非如此。
const { camel } = window.radash;
const camellizeObject = R.pipe(
R.toPairs,
R.map(([key, val]) => [camel(key), val]),
R.fromPairs,
);
const camellize = (input /* obj | array */) => R.map(
camellizeObject,
// the [].concat(input) could help you save that cond
([] /* as Record<string, unknown>[] */).concat(input),
);
console.log(
camellize({ snaked_name: 'Giuseppe', snaked_surname: 'Mandato' }),
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.30.1/ramda.js" integrity="sha512-FHBrUNR2Aspc5CQI8/3KQAmtud6erZKGG+BBdT5EqL2i1OcKP9POVu2ZLVDAMxmoc7go1kIWnq6WzmRjdsOxTw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/radash/12.1.0/radash.min.js" integrity="sha512-JEUqDJrck58H46FvJVzwvDWVlVEF83BkaG8/fagfTApgdo8ERV+Hqx4t7rDyFN8JGv84+r+WNoq9oZsgi5Glhw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>