返回值类型的 TypeScript 问题
TypeScript problem with type on returned value
我收到此 tslint 错误:
TS2322: Type 'ItemList | undefined' is not assignable to type 'Transaction<any, any, any>[]'. Type 'undefined' is not assignable to type 'Transaction<any, any, any>[]'.
我有这个代码:
import { Transaction } from 'types';
import { DocumentClient as DynamoDBDocumentClient } from 'aws-sdk/clients/dynamodb';
export default async function getMyTransactions (
_root: any,
args: null,
): Promise<Array<Transaction<any, any, any>>> {
//some code for query and other stuff
const { Items } = await DocumentClient.getInstance().query(query).promise();
console.log('Items', Items);
return Items;
}
在 console.log(Items)
我得到这样的东西:
[
{
id: '123',
commissions: {
cc: 500,
total: 1337
},
createdAt: '2001-02-03T04:05:06+07',
]
Transaction
类型是这个:
// I wont put the whole code, but those TF, TV, TC are used on other types not listed here
export interface Transaction<TF, TV, TC> {
id: string;
commissions: Commissions;
createdAt: string;
}
因此,在函数的开头我声明它将 return:Promise<Array<Transaction<any, any, any>>>
所以Items
实际上是Transaction
类型对象的数组
为什么我会收到错误消息?
正如您在 typings 中看到的那样,Items
属性 可以是未定义的,因为您在 tsconfig.json
中使用 strict
设置将导致错误。所以一个简单的解决方法是总是 return 一个数组:
export default async function getMyTransactions (
_root: any,
args: null,
): Promise<Array<Transaction<any, any, any>>> {
// ...
const { Items } = await DocumentClient.getInstance().query(query).promise();
return Items || [];
}
或者您必须更新您的 return 键入以反映 Items
可以是什么,并且您应该在您的方法的使用者处处理此未定义:
Promise<Array<Transaction<any, any, any>> | undefined>
不幸的是,当您查询数据时,dynamodb 的类型没有任何泛型。我可以假设您必须执行以下操作才能使其工作:
export interface Transaction<TF, TV, TC> extends AttributeMap {
id: string;
commissions: Commissions;
createdAt: string;
}
也许您必须专门对其进行类型转换,但不确定:
const { Items } = await DocumentClient.getInstance().query(query).promise();
return Items || [] as Transaction<any, any, any>[];
我收到此 tslint 错误:
TS2322: Type 'ItemList | undefined' is not assignable to type 'Transaction<any, any, any>[]'. Type 'undefined' is not assignable to type 'Transaction<any, any, any>[]'.
我有这个代码:
import { Transaction } from 'types';
import { DocumentClient as DynamoDBDocumentClient } from 'aws-sdk/clients/dynamodb';
export default async function getMyTransactions (
_root: any,
args: null,
): Promise<Array<Transaction<any, any, any>>> {
//some code for query and other stuff
const { Items } = await DocumentClient.getInstance().query(query).promise();
console.log('Items', Items);
return Items;
}
在 console.log(Items)
我得到这样的东西:
[
{
id: '123',
commissions: {
cc: 500,
total: 1337
},
createdAt: '2001-02-03T04:05:06+07',
]
Transaction
类型是这个:
// I wont put the whole code, but those TF, TV, TC are used on other types not listed here
export interface Transaction<TF, TV, TC> {
id: string;
commissions: Commissions;
createdAt: string;
}
因此,在函数的开头我声明它将 return:Promise<Array<Transaction<any, any, any>>>
所以Items
实际上是Transaction
类型对象的数组
为什么我会收到错误消息?
正如您在 typings 中看到的那样,Items
属性 可以是未定义的,因为您在 tsconfig.json
中使用 strict
设置将导致错误。所以一个简单的解决方法是总是 return 一个数组:
export default async function getMyTransactions (
_root: any,
args: null,
): Promise<Array<Transaction<any, any, any>>> {
// ...
const { Items } = await DocumentClient.getInstance().query(query).promise();
return Items || [];
}
或者您必须更新您的 return 键入以反映 Items
可以是什么,并且您应该在您的方法的使用者处处理此未定义:
Promise<Array<Transaction<any, any, any>> | undefined>
不幸的是,当您查询数据时,dynamodb 的类型没有任何泛型。我可以假设您必须执行以下操作才能使其工作:
export interface Transaction<TF, TV, TC> extends AttributeMap {
id: string;
commissions: Commissions;
createdAt: string;
}
也许您必须专门对其进行类型转换,但不确定:
const { Items } = await DocumentClient.getInstance().query(query).promise();
return Items || [] as Transaction<any, any, any>[];