【发布时间】:2020-12-06 10:29:48
【问题描述】:
我想为函数创建一个可重用的类型,所以我可以说:
type MyFunc = ...
const func1: MyFunc = ...
const func2: MyFunc = ...
以下不起作用,但基本上我想要:
type Book = {
id: string;
title: string;
}
type createBook = (book: Partial<Book>) => Book;
const someBookFactory: createBook = ({ title }) => ({ id: /* autogenerated */, title });
const someOtherBookFactory: createBook = ({ id, title }) => ({ id, title });
这在 TypeScript 中可行吗?
【问题讨论】:
标签: typescript function typescript-typings factory