diff --git a/types/ejs/ejs-tests.ts b/types/ejs/ejs-tests.ts index a7b7b66944..8a0b2404d7 100644 --- a/types/ejs/ejs-tests.ts +++ b/types/ejs/ejs-tests.ts @@ -3,7 +3,7 @@ import ejs = require("ejs"); import { readFileSync as read } from 'fs'; import LRU = require("lru-cache"); -import { TemplateFunction, Options } from "ejs"; +import { TemplateFunction, AsyncTemplateFunction, Options } from "ejs"; const fileName = 'test.ejs'; const people = ['geddy', 'neil', 'alex']; @@ -12,6 +12,8 @@ const template = '<%= people.join(", "); %>'; const options = { filename: fileName }; let result: string; let ejsFunction: TemplateFunction; +let asyncResult: Promise; +let ejsAsyncFunction: AsyncTemplateFunction; const SimpleCallback = (err: any, html?: string) => { if (err) { @@ -44,11 +46,19 @@ ejsFunction = ejs.compile('<%= it.people.join(", "); %>', { _with: false, locals ejsFunction = ejs.compile(template, { rmWhitespace: true }); const customEscape = (str: string) => !str ? '' : str.toUpperCase(); ejsFunction = ejs.compile(template, { escape: customEscape }); +ejsFunction = ejs.compile(template, { async: false }); + +ejsAsyncFunction = ejs.compile(template, { async: true }); +ejsAsyncFunction = ejs.compile(template, { client: true, async: true }); result = ejsFunction(); result = ejsFunction({}); result = ejsFunction(data); +asyncResult = ejsAsyncFunction(); +asyncResult = ejsAsyncFunction({}); +asyncResult = ejsAsyncFunction(data); + /** @see https://github.com/mde/ejs/tree/v2.5.7#custom-fileloader */ ejs.fileLoader = (path: string) => ""; diff --git a/types/ejs/index.d.ts b/types/ejs/index.d.ts index e56ad1a8ec..655d6563e6 100644 --- a/types/ejs/index.d.ts +++ b/types/ejs/index.d.ts @@ -2,7 +2,7 @@ // Project: http://ejs.co/ // Definitions by: Ben Liddicott // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 +// TypeScript Version: 2.8 export interface Data { [name: string]: any; @@ -28,14 +28,22 @@ export function resolveInclude(name: string, filename: string, isDir: boolean): /** * Compile the given `str` of ejs into a template function. */ -export function compile(template: string, opts?: Options): (TemplateFunction); +export function compile(template: string, opts?: Options & { async: false }): TemplateFunction; +export function compile(template: string, opts: Options & { async: true }): AsyncTemplateFunction; +export function compile(template: string, opts: Options & { async: boolean }): TemplateFunction | AsyncTemplateFunction; +export function compile(template: string, opts: Exclude): TemplateFunction; +export function compile(template: string, opts?: Options): TemplateFunction | AsyncTemplateFunction; /** * Render the given `template` of ejs. * * If you would like to include options but not data, you need to explicitly * call this function with `data` being an empty object or `null`. */ -export function render(template: string, data?: Data, opts?: Options): string; +export function render(template: string, data?: Data, opts?: Options & { async: false }): string; +export function render(template: string, data: Data, opts: Options & { async: true }): Promise; +export function render(template: string, data: Data, opts: Options & { async: boolean }): string | Promise; +export function render(template: string, data: Data, opts: Exclude): string; +export function render(template: string, data?: Data, opts?: Options): string | Promise; export type RenderFileCallback = (err: Error, str?: string) => T; @@ -55,6 +63,7 @@ export function renderFile(path: string, data: Data, opts: Options, cb: Rende export function clearCache(): void; export type TemplateFunction = (data?: Data) => string; +export type AsyncTemplateFunction = (data?: Data) => Promise; export interface Options { /** Compiled functions are cached, requires `filename` */ cache?: boolean; @@ -95,6 +104,8 @@ export interface Options { * (By default escapes XML). */ escape?(str: string): string; + /** Whether or not to use a Promise */ + async?: boolean; } export function escapeRegexChars(s: string): string;