JSPM

@rbxts/ty

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 5
    • Score
      100M100P100Q31821F
    • License ISC

    Type validation with detailed errors

    Package Exports

      This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rbxts/ty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

      Readme

      @rbxts/ty

      Type validation with detailed errors.

      Alternative to @rbxts/t.

      Basics

      import ty from "@rbxts/ty";
      
      {
        const result = ty.string("hello!");
        if (result.success)
          print(result.value) // hello!
      }
      {
        const result = ty.string(69);
        if (!result.success)
          print(result.errors[0].message) // Expected 'string', got: 69
      }

      Literals

      import ty from "@rbxts/ty";
      
      const abcGuard = ty.literal("abc");
      {
        const result = abcGuard("abc");
        if (result.success)
          print(result.value) // abc
      }
      {
        const result = abcGuard(69);
        if (!result.success)
          print(result.errors[0].message) // Expected '"abc"', got: 69
      }

      Objects

      const weaponGuard = ty.object({
        name: ty.string,
        damage: ty.number
      }, "Weapon");
      
      {
        const result = weaponGuard({
          name: "Gun",
          damage: 50
        });
        if (result.success)
          print(result.value) // { name = "Gun", damage = 50 }
      }
      {
        const result = weaponGuard(69);
        if (!result.success)
          print(result.value) // Expected 'Weapon', got: 69
      }
      {
        const result = weaponGuard({ name: "Fists" });
        if (!result.success)
          print(result.value) // Expected 'number', got: nil (Weapon.damage)
      }

      Unions

      const isStringOrNumber = ty.union(ty.string, ty.number);
      
      {
        const result = isStringOrNumber(69);
        if (result.success)
          print(result.value) // 69
      }
      {
        const result = isStringOrNumber("abc");
        if (result.success)
          print(result.value) // abc
      }
      {
        const result = isStringOrNumber(true);
        if (!result.success)
          print(result.value) // Expected 'string | number', got: true
      }

      Intersections

      const isFooBar = ty.intersection(
        ty.object({
          a: ty.string
        }, "Foo"),
        ty.object({
          b: ty.number
        }, "Bar")
      );
      
      {
        const result = isFooBar({ a: "abc", b: 69 });
        if (result.success)
          print(result.value) // { a = "abc", b = 69 }
      }
      {
        const result = isFooBar({ a: "abc" });
        if (!result.success)
          print(result.value) // Expected 'number', got nil (Bar.b)
      }