[英]TypeScript: How to declare type for Record or Array of Records?
Suppose there's an apiCall
function which returns a promise that resolves as an object that has a data
property, which can be either a Record
or an array of Record
s (like Axios).假设有一个
apiCall
function,它返回一个 promise,它解析为一个 object,它具有一个data
属性(如 Ax Record
或Record
数组) I'm trying to declare the types appropriately, but I'm getting an error.我正在尝试适当地声明类型,但出现错误。
Here's how I declare my types:这是我声明我的类型的方式:
type ApiResponseType = {
data: Record<string, any>[] | Record<string, any>;
};
function apiCall(asArr: boolean): Promise<ApiResponseType> {
const d = {
foo: 'bar',
};
return Promise.resolve({
data: asArr ? [d] : d,
});
}
But when I use it like so:但是当我这样使用它时:
(async()=>{
let a: {[key: string]: any} | null = null;
let b: {[key: string]: any}[] | null = null;
a = (await apiCall(false)).data;
b = (await apiCall(true)).data; // Type 'Record<string, any> | Record<string, any>[]' is not assignable to type '{[key: string]: any;}[] | null'. Type 'Record<string, any>' is missing the following properties from type '{[key: string]: any;}[]': length, pop, push, concat, and 26 more.
console.log({a, b});
})();
I get the following error:我收到以下错误:
Type 'Record<string, any> |
键入'记录<字符串,任何> | Record<string, any>[]' is not assignable to type '{[key: string]: any;}[] |
Record<string, any>[]' 不可分配给类型 '{[key: string]: any;}[] | null'.
无效的'。 Type 'Record<string, any>' is missing the following properties from type '{[key: string]: any;}[]': length, pop, push, concat, and 26 more.
类型 'Record<string, any>' 缺少来自类型 '{[key: string]: any;}[]' 的以下属性:length、pop、push、concat 和另外 26 个。
I think it may have something to do with the array being treated as a Record<string, any>我认为这可能与被视为 Record<string, any> 的数组有关
This is because:这是因为:
declare var a: Record<string, any>[]
declare var b: Record<string, any>
a = b // error
b = a // ok
Array of Records is assignable to Record but not vice - versa.记录数组可分配给记录,但反之亦然。
But let's go back to your example:但是让我们 go 回到你的例子:
type Data = Record<string, any>[] | Record<string, any>;
type Nullable<T> = T | null
type ApiResponseType<T extends Data> = {
data: T
};
function apiCall(asArr: true): Promise<ApiResponseType<Record<string, any>[]>>
function apiCall(asArr: false): Promise<ApiResponseType<Record<string, any>>>
function apiCall(asArr: boolean) {
const d = {
foo: 'bar',
};
return Promise.resolve<ApiResponseType<Data>>({
data: asArr ? [d] : d,
});
}
type O = ReturnType<typeof apiCall>
(async () => {
let a: Nullable<Record<string, any>> = null;
let b: Nullable<Record<string, any>[]> = null;
a = (await apiCall(false)).data;
b = (await apiCall(true)).data;
// without LET bindings
{
const a = (await apiCall(false)).data; // Record<string, any>
const b = (await apiCall(true)).data; //Record<string, any>[]
}
})();
With help of overloadings, we can give some hint to compiler.借助重载,我们可以给编译器一些提示。
Please see this section: // without LET bindings
请参阅本节:
// without LET bindings
Compiler is able to figure out ReturnType of function编译器能够找出 function 的 ReturnType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.