When extending APIs with new capabilities, carefully consider how changes to return values affect consumers. Avoid creating polymorphic return types based on input options as this reduces predictability and makes it difficult to programmatically detect supported features.
When extending APIs with new capabilities, carefully consider how changes to return values affect consumers. Avoid creating polymorphic return types based on input options as this reduces predictability and makes it difficult to programmatically detect supported features.
Instead, follow these patterns:
Symbol.dispose
)mkdtemp
โ mkdtempDisposable
)// AVOID: Polymorphic returns based on options
function createTempDir(prefix, { disposable = false } = {}) {
const path = makeTempDir(prefix);
return disposable ?
{ path, [Symbol.dispose]: () => removeTempDir(path) } :
path;
}
// BETTER: Separate, clearly named APIs
function createTempDir(prefix) {
return makeTempDir(prefix);
}
function createDisposableTempDir(prefix) {
const path = makeTempDir(prefix);
return {
path,
[Symbol.dispose]: () => removeTempDir(path)
};
}
When introducing new return types:
Enter the URL of a public GitHub repository