Skip to main content
Module

x/scaffold/src/deps/types.ts>Replace

scaffold your next project with style and 💗
Latest
type alias Replace
import { type Replace } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

Represents a string with some or all matches replaced by a replacement.

Use-case:

  • snake-case-path to dotted.path.notation
  • Changing date/time format: 01-08-204201/08/2042
  • Manipulation of type properties, for example, removal of prefixes

Examples

Example 1

import {Replace} from 'type-fest';

declare function replace<
	Input extends string,
	Search extends string,
	Replacement extends string
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement>;

declare function replaceAll<
	Input extends string,
	Search extends string,
	Replacement extends string
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement, {all: true}>;

// The return type is the exact string literal, not just `string`.

replace('hello ?', '?', '🦄');
//=> 'hello 🦄'

replace('hello ??', '?', '❓');
//=> 'hello ❓?'

replaceAll('10:42:00', ':', '-');
//=> '10-42-00'

replaceAll('__userName__', '__', '');
//=> 'userName'

replaceAll('My Cool Title', ' ', '');
//=> 'MyCoolTitle'

Type Parameters

Input extends string
Replacement extends string
optional
Options extends ReplaceOptions = { }
definition: Input extends `${infer Head}${Search}${infer Tail}` ? Options["all"] extends true ? Replace<`${Head}${Replacement}${Tail}`, Search, Replacement, Options> : `${Head}${Replacement}${Tail}` : Input