cf.completefrontend

Write the codeMedium

tiny-slug ships no types. Write a declaration so import { slugify } from "tiny-slug" is fully typed.

The package exports one function, slugify(text, options?), which returns a string. options can have separator (a string) and lower (a boolean). There's no @types/tiny-slug. The shorthand declaration in types/tiny-slug.d.ts silences the import error, but everything from the package is any. Replace it with a declaration that types the function and exports its options type as SlugifyOptions.

node_modules/tiny-slug/package.json
{ "name": "tiny-slug", "version": "2.1.0", "main": "index.js" }
node_modules/tiny-slug/index.js
exports.slugify = function slugify(text, options = {}) {
  const separator = options.separator ?? "-";
  const slug = text.trim().split(/[^A-Za-z0-9]+/).filter(Boolean).join(separator);
  return options.lower === false ? slug : slug.toLowerCase();
};
src/main.ts
import { slugify } from "tiny-slug";
 
console.log(slugify("Hello World")); // "hello-world"
Write this (types/tiny-slug.d.ts)
declare module "tiny-slug";

Solve it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…