Skip to content

Instantly share code, notes, and snippets.

@DarkGL
Last active July 13, 2024 19:01
Show Gist options
  • Select an option

  • Save DarkGL/3c8f70a194447d64fad4c3941c8a03cc to your computer and use it in GitHub Desktop.

Select an option

Save DarkGL/3c8f70a194447d64fad4c3941c8a03cc to your computer and use it in GitHub Desktop.

Revisions

  1. DarkGL revised this gist May 19, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lowercase.ts
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ const normalizedInput: SafeLowercaseString = toLowercaseString("Hello World");
    const normalString: string = normalizedInput as string;
    normalString.toLowerCase(); // This is fine, but now it's not type-checked as a lowercase string

    /*-------------------------------------------*/
    /*------------------------------------------------------------------------------------------------------------*/

    // Step 1: Define the Branded Type
    type SafeLowercaseString = Omit<string, "toLowerCase"> & { __brand: "LowercaseString" };
  2. DarkGL revised this gist May 19, 2024. No changes.
  3. DarkGL revised this gist May 19, 2024. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions lowercase.ts
    Original file line number Diff line number Diff line change
    @@ -20,3 +20,23 @@ const normalizedInput: SafeLowercaseString = toLowercaseString("Hello World");
    // To use it as a normal string, you need to explicitly cast it back
    const normalString: string = normalizedInput as string;
    normalString.toLowerCase(); // This is fine, but now it's not type-checked as a lowercase string

    /*-------------------------------------------*/

    // Step 1: Define the Branded Type
    type SafeLowercaseString = Omit<string, "toLowerCase"> & { __brand: "LowercaseString" };

    // Step 2: Type Guard Function
    function toLowercaseString(s: string): SafeLowercaseString {
    return s.toLowerCase() as unknown as SafeLowercaseString;
    }

    // Usage
    const normalizedInput = toLowercaseString("Hello World");

    // Trying to call toLowerCase() on normalizedInput will result in a TypeScript error
    // normalizedInput.toLowerCase(); // Error: Property 'toLowerCase' does not exist on type 'string & { __brand: "LowercaseString"; }'.

    // To use it as a normal string, you need to explicitly cast it back
    const normalString: string = normalizedInput as string;
    normalString.toLowerCase(); // This is fine, but now it's not type-checked as a lowercase string
  4. DarkGL revised this gist May 19, 2024. No changes.
  5. DarkGL created this gist May 19, 2024.
    22 changes: 22 additions & 0 deletions lowercase.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // Step 1: Define the Branded Type
    type LowercaseString = string & { __brand: "LowercaseString" };

    // Step 2: Type Guard Function
    function toLowercaseString(s: string): LowercaseString {
    return s.toLowerCase() as LowercaseString;
    }

    // Step 3: Override Type Definitions
    type RemoveToLowerCase<T> = Omit<T, "toLowerCase">;

    type SafeLowercaseString = RemoveToLowerCase<LowercaseString>;

    // Usage
    const normalizedInput: SafeLowercaseString = toLowercaseString("Hello World");

    // Trying to call toLowerCase() on normalizedInput will result in a TypeScript error
    // normalizedInput.toLowerCase(); // Error: Property 'toLowerCase' does not exist on type 'string & { __brand: "LowercaseString"; }'.

    // To use it as a normal string, you need to explicitly cast it back
    const normalString: string = normalizedInput as string;
    normalString.toLowerCase(); // This is fine, but now it's not type-checked as a lowercase string