TypeScript接口中能否定义函数?Svelte可写存储场景需求
TypeScript接口能否在内部定义函数?
我是TypeScript接口使用的新手,请问是否可以像如下代码这样在接口内部定义函数?以下是我global.d.ts文件中的当前代码,我希望在接口中定义函数,用于赋值给Svelte框架中的可写存储。补充说明:代码中的this.调用原本来自类,我想通过它们引用接口中的上述属性。
type Sorting = { col: string, ascending: boolean }; type Paginate = (items: Results) => Promise<any>; type SetPage = (page: number) => Promise<any>; type Sort = (column: string, data:Results, callback:CallableFunction) => Promise<any>; interface IPagination { page: number = 0; totalPages: [] = []; currentRow: [] = []; itemsPerPage: number = 5; sortBy: Sorting = { col: "customername", ascending: true }; Paginate: Paginate = ((items:Results) => { const pages = Math.ceil(items.length / this.itemsPerPage); const paginatedItems = Array.from({ length: pages }, (_, index) => { const start = index * this.itemsPerPage; return items.slice(start, start + this.itemsPerPage); }); this.totalPages = [...paginatedItems]; this.TotalPages = this.totalPages; return this.totalPages; }); SetPage: Set; Sort: Sort; }
解答
首先明确:TypeScript接口只能定义结构(属性和方法的签名),不能包含默认值、函数实现,你当前的写法完全不符合接口的语法规则,会直接报错。
针对你的需求(给Svelte可写存储赋值,保留类中this引用属性的逻辑),可以用以下两种方案:
方案1:用类实现接口
先定义标准接口描述结构,再用类实现它,类中可以设置默认值和编写函数逻辑,最后实例化类得到对象赋值给可写存储:
type Sorting = { col: string, ascending: boolean }; type Results = any[]; // 需补充Results的实际类型,原代码未定义 type PaginateFn = (items: Results) => Promise<any>; type SetPageFn = (page: number) => Promise<any>; type SortFn = (column: string, data: Results, callback: CallableFunction) => Promise<any>; // 定义标准接口,仅描述结构 interface IPagination { page: number; totalPages: any[]; currentRow: any[]; itemsPerPage: number; sortBy: Sorting; paginate: PaginateFn; setPage: SetPageFn; sort: SortFn; } // 用类实现接口,添加默认值与函数逻辑 class Pagination implements IPagination { page = 0; totalPages: any[] = []; currentRow: any[] = []; itemsPerPage = 5; sortBy: Sorting = { col: "customername", ascending: true }; async paginate(items: Results) { const pages = Math.ceil(items.length / this.itemsPerPage); const paginatedItems = Array.from({ length: pages }, (_, index) => { const start = index * this.itemsPerPage; return items.slice(start, start + this.itemsPerPage); }); this.totalPages = [...paginatedItems]; return this.totalPages; } async setPage(page: number) { // 补充你的setPage逻辑 this.page = page; this.currentRow = this.totalPages[page] || []; } async sort(column: string, data: Results, callback: CallableFunction) { // 补充你的sort逻辑 this.sortBy = { col: column, ascending: !this.sortBy.ascending }; callback(); } } // 实例化后赋值给Svelte可写存储 import { writable } from 'svelte/store'; const paginationStore = writable(new Pagination());
方案2:直接创建符合接口的对象字面量
如果不需要类的继承特性,可直接创建符合接口结构的对象,用闭包维护内部状态替代类的this:
type Sorting = { col: string, ascending: boolean }; type Results = any[]; type PaginateFn = (items: Results) => Promise<any>; type SetPageFn = (page: number) => Promise<any>; type SortFn = (column: string, data: Results, callback: CallableFunction) => Promise<any>; interface IPagination { page: number; totalPages: any[]; currentRow: any[]; itemsPerPage: number; sortBy: Sorting; paginate: PaginateFn; setPage: SetPageFn; sort: SortFn; } // 创建符合接口的对象 const createPagination = (): IPagination => { let page = 0; let totalPages: any[] = []; let currentRow: any[] = []; const itemsPerPage = 5; let sortBy: Sorting = { col: "customername", ascending: true }; return { get page() { return page; }, set page(val) { page = val; }, get totalPages() { return totalPages; }, set totalPages(val) { totalPages = val; }, get currentRow() { return currentRow; }, set currentRow(val) { currentRow = val; }, itemsPerPage, get sortBy() { return sortBy; }, set sortBy(val) { sortBy = val; }, async paginate(items: Results) { const pages = Math.ceil(items.length / itemsPerPage); const paginatedItems = Array.from({ length: pages }, (_, index) => { const start = index * itemsPerPage; return items.slice(start, start + itemsPerPage); }); totalPages = [...paginatedItems]; return totalPages; }, async setPage(newPage: number) { page = newPage; currentRow = totalPages[page] || []; }, async sort(column: string, data: Results, callback: CallableFunction) { sortBy = { col: column, ascending: !sortBy.ascending }; callback(); } }; }; // 赋值给Svelte可写存储 import { writable } from 'svelte/store'; const paginationStore = writable(createPagination());
关键注意点
- 接口的核心作用是类型约束,仅定义“有什么属性/方法”,不能定义“属性默认值”或“方法具体实现”。
- 原代码中
SetPage: Set;是错误写法,Set是TS内置类型,应使用自定义的SetPage类型;同时注意TS命名规范:类型名首字母大写,属性/方法名小驼峰。 - 原代码未定义
Results类型,需补充实际类型以避免any弱化类型检查。
内容的提问来源于stack exchange,提问作者JustBarnt
相关产品推荐
相关产品推荐

