Skip to content

v0.6.0

Compare
Choose a tag to compare
@GoogleFeud GoogleFeud released this 01 Jun 14:38
· 7 commits to dev since this release

Additions

  • Transformations - You can now add transformation markers to types and use the transform utility function to perform the transformations:
    function timestampToDate(ts: number) : Date {
        return new Date(ts);
    }
    
    function transformAge(age: number) : number {
        return age + 1;
    }
    
    type User = {
        username: string,
        createdAt: Transform<typeof timestampToDate>,
        age: string & Transform<["+$self", typeof transformAge]>
    }
    
    const myUser: User = {
        username: "GoogleFeud",
        createdAt: 1716657364400,
        age: "123"
    }
    
    console.log(transform<User>(myUser))
    
    // Transpiles to:
    let result_1;
    result_1 = {};
    result_1.createdAt = timestampToDate(myUser.createdAt);
    result_1.age = transformAge(+myUser.age);
    result_1.username = myUser.username;
    console.log(result_1);
  • Undefined and Null types which can be used with conditional transformations and checks - null & Transform<...> is never, so these types need to exist.
  • PostCheck utility type which can be used alongside Transform to perform validation of the transformed values.

Changes

  • The playground now uses astro instead of next.js, and it's URL has been changed to googlefeud.github.io/ts-runtime-checks/playground. I'll be writing better and more interactive documentation over the next few weeks!
  • You'll no longer see stringified typescript types inside errors, they've been made a lot more readable.
  • Union type data now includes the possible variants in raw errors.
  • You can now use unions with check types: (implements #40)
    function validate1(
      param: Assert<Min<10> & Max<30> | string & (StartsWith<"a"> | MinLen<3>) | number[] & MinLen<3>>
    ) {
      return true;
    }
    
    // Transpiles to:
    function validate1(param) {
        if ((typeof param !== "string" || !param.startsWith("a") && param.length < 3) && (typeof param !== "number" || param < 10 || param > 30))
            if (!Array.isArray(param) || param.length < 3)
                throw new Error("Expected param to be number, to be greater than 10 & to be less than 30 | string, to start with \"a\" or to have a length greater than 3 | array<number>, to have a length greater than 3");
            else {
                for (let i_1 = 0; i_1 < param.length; i_1++) {
                    if (typeof param[i_1] !== "number")
                        throw new Error("Expected param[" + i_1 + "] to be a number");
                }
            }
        return true;
    }

Breaking changes

  • Removed the Or utility type - use the | operator.
  • Changed the Eq utility type to be more flexible, its parameters have been reversed, and the type is now optional.