diff --git a/index.d.ts b/index.d.ts index 09f31bd25..edf874109 100644 --- a/index.d.ts +++ b/index.d.ts @@ -546,7 +546,7 @@ declare module 'egg' { headers: { [key: string]: string }; } - export interface Router extends KoaRouter { + export interface Router extends Omit, 'url'> { /** * restful router api */ @@ -554,7 +554,7 @@ declare module 'egg' { /** * @param {String} name - Router name - * @param {Object} params - more parameters + * @param {Object} [params] - more parameters * @example * ```js * router.url('edit_post', { id: 1, name: 'foo', page: 2 }) @@ -565,7 +565,12 @@ declare module 'egg' { * @return {String} url by path name and query params. * @since 1.0.0 */ - url(name: string, params: any): any; + url(name: string, params?: any): string; + /** + * Alias for the url method + */ + pathFor(name: string, params?: any): string; + methods: string[]; } export interface EggApplication extends EggCoreBase { // tslint:disable-line diff --git a/test/fixtures/apps/app-ts/app/controller/foo.ts b/test/fixtures/apps/app-ts/app/controller/foo.ts index ef7541387..1ec36c780 100644 --- a/test/fixtures/apps/app-ts/app/controller/foo.ts +++ b/test/fixtures/apps/app-ts/app/controller/foo.ts @@ -30,6 +30,13 @@ export default class FooController extends Controller { this.fooLogger = ctx.getLogger('foo'); assert(ctx.app.ctxStorage); assert(ctx.app.currentContext); + + // router + console.log(ctx.app.router.url('foo')); + console.log(ctx.app.router.url('foo', {})); + console.log(ctx.app.router.pathFor('foo')); + console.log(ctx.app.router.pathFor('foo', {})); + console.log(ctx.app.router.methods); } async getData() {