Type alias Replace<Value, Target, Replacement, Global>

Replace<Value, Target, Replacement, Global>: Value extends string
    ? Target extends string
        ? Replacement extends string
            ? Value extends `${infer Head extends string}${Target}${infer Tail extends string}`
                ? `${Head}${Replacement}${Global extends true
                    ? Replace<Tail, Target, Replacement>
                    : Tail}`
                : Value
            : Value
        : Value
    : Value

Replace

Type Parameters

  • Value extends string

    The value to replace

  • Target extends string

    The target to replace

  • Replacement extends string = ""

    [=''] - The replacement string

  • Global extends boolean = true

    [true] - Replace all occurrences

Description

Replace a string with another string

Returns

The replaced string

Example

type Value = 'foobar';
type Target = 'foo';
type Replacement = 'bar';
type Result = Replace<Value, Target, Replacement>; // 'barbar'

Example

type Value = 'foofoofoo';
type Target = 'foo';
type Replacement = 'bar';
type Result = Replace<Value, Target, Replacement, false>; // 'barfoofoo'