我嘗試從這個博客復制這段代碼,但遇到了一些非常模糊的錯誤
import { option, identity, apply } from 'fp-ts';
import type { Kind, URIS } from 'fp-ts/HKT';
import { pipe } from 'fp-ts/lib/function';
type OrderId = string;
type OrderState = 'active' | 'deleted';
interface User {
readonly name: string;
readonly isActive: boolean;
}
interface OrderHKD<F extends URIS> {
readonly id: Kind<F, OrderId>;
readonly issuer: Kind<F, User>;
readonly date: Kind<F, Date>;
readonly comment: Kind<F, string>;
readonly state: Kind<F, OrderState>;
}
type Order = OrderHKD<identity.URI>;
type OrderOption = OrderHKD<option.URI>;
const validateOrder = (inputOrder: OrderOption): option.Option<Order> =>
pipe(inputOrder, apply.sequenceS(option.Apply));
// ^
// | here
// Argument of type '<NER extends Record<string, Option<any>>>(r: EnforceNonEmptyRecord<NER>) => Option<{ [K in keyof NER]: [NER[K]] extends [Option<infer A>] ? A : never; }>' is not assignable to parameter of type '(a: OrderOption) => Option<{ [x: string]: any; }>'.
// Types of parameters 'r' and 'a' are incompatible.
// Type 'OrderOption' is not assignable to type 'Record<string, Option<any>>'.
// Index signature for type 'string' is missing in type 'OrderHKD<"Option">'.ts(2345)
uj5u.com熱心網友回復:
我不知道這段代碼是如何作業的,因為它的結果sequenceS有一個約束<NER extends Record<string, Kind<F, any>>>,由于可能的宣告合并,介面無法滿足這個約束(例如,參見這個 TypeScript 問題)。而這種限制在 2019 年就已經存在,那是在寫博客文章之前。
無論如何,您可以通過宣告OrderHKD為 atype而不是a 來使示例作業interface:
type OrderHKD<F extends URIS> = {
readonly id: Kind<F, OrderId>;
readonly issuer: Kind<F, User>;
readonly date: Kind<F, Date>;
readonly comment: Kind<F, string>;
readonly state: Kind<F, OrderState>;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/444753.html
上一篇:如何將型別轉換為查詢字串?
