PokéRogue
    Preparing search index...

    Type Alias IsStringLiteral<S>

    IsStringLiteral: IfNotAnyOrNever<
        S,
        _IsStringLiteral<
            CollapseLiterals<S extends TagContainer<any> ? UnwrapTagged<S> : S>,
        >,
        false,
        false,
    >

    Returns a boolean for whether the given type is a string literal type.

    Useful for: - providing strongly-typed string manipulation functions - constraining strings to be a string literal - type utilities, such as when constructing parsers and ASTs

    The implementation of this type is inspired by the trick mentioned in this StackOverflow answer.

    Type Parameters

    • S
    import type {IsStringLiteral} from 'type-fest';

    type CapitalizedString<T extends string> = IsStringLiteral<T> extends true ? Capitalize<T> : string;

    // https://github.com/yankeeinlondon/native-dash/blob/master/src/capitalize.ts
    function capitalize<T extends Readonly<string>>(input: T): CapitalizedString<T> {
    return (input.slice(0, 1).toUpperCase() + input.slice(1)) as CapitalizedString<T>;
    }

    const output = capitalize('hello, world!');
    //=> 'Hello, world!'
    // String types with infinite set of possible values return `false`.

    import type {IsStringLiteral} from 'type-fest';

    type AllUppercaseStrings = IsStringLiteral<Uppercase<string>>;
    //=> false

    type StringsStartingWithOn = IsStringLiteral<`on${string}`>;
    //=> false

    // This behaviour is particularly useful in string manipulation utilities, as infinite string types often require separate handling.

    type Length<S extends string, Counter extends never[] = []> =
    IsStringLiteral<S> extends false
    ? number // return `number` for infinite string types
    : S extends `${string}${infer Tail}`
    ? Length<Tail, [...Counter, never]>
    : Counter['length'];

    type L1 = Length<Lowercase<string>>;
    //=> number

    type L2 = Length<`${number}`>;
    //=> number