Repository navigation
Releases: volenslavchev/ts-runtime-checks
Releases · volenslavchev/ts-runtime-checks
Release list
v0.6.3
Additions
- Markers are now removed from typescript declaration files. For this to work, you must set the
afterDeclarationsplugin option totrue. Important: If you set this option to true, then your normal .ts files won't get transformed, so you'll have to add this transformer twice, once withafterDeclarationsset to true, and once set to false.
Bug fixes
- Negative numbers get generated correctly (#68)
- Support for special characters in property names (#63)
Changes
- Support for typescript
5.7.x
v0.6.2
Additions
assertAllconfig option - Setting this option to true will add assertion code to ALL function parameters andasassertions. The assertion code will throw an error. You can use theNoCheckmarker to override this behaviour. (#61)
Changes
- Support for typescript
5.6.x
v0.6.1
Changes
- Support for typescript
5.5.x(fixes #59)
v0.6.0
Additions
- Transformations - You can now add transformation markers to types and use the
transformutility 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);
UndefinedandNulltypes which can be used with conditional transformations and checks - null & Transform<...> isnever, so these types need to exist.PostCheckutility type which can be used alongsideTransformto 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
Orutility type - use the|operator. - Changed the
Equtility type to be more flexible, its parameters have been reversed, and the type is now optional.
v0.5.1
Bug fixes
- Fixes #53
Changes
- Support typescript
5.4.x - Tiny performance optimizations
- Mostly tweaking of weight values
typeof value === 'boolean'=>value === true || value === false
v0.5.0
Additions
-
Checkmarker now accepts functions as well as string expressions:function complexValidation(value: unknown) { // Do validation... } type ComplexValidator = Check<typeof complexValidation, "to pass complex validation">; function validate(user: Assert<ComplexValidator>) { // Your code... } validate('1234' as ComplexValidator);
Transpiles to:
function complexValidation(value) {} function validate(user) { if (!complexValidation(user)) throw new Error("Expected user to pass complex validation."); } validate('1234');
-
The transformer now handles importing of values used inside markers. This means that the transformer is automatically going to import error classes, as well as functions used inside
Checks. However, it's unable to import default exports, so make sure the stuff you pass to markers isn't exported as default.
Bug fixes
- Mostly fixes #47 (see above for more info)
Changes
Min,Max,MinLenandMaxLenare now inclusive (#51)
v0.4.2
v0.4.1
- Support for typescript
5.2.x.
Changes
- You can now combine multiple
Checks without a base type. - Updated playground to display all logged messages.
Bug fixes
- Fix #35.
v0.4.0
Additions
-
A
createMatchutility function that creates pattern-matching functions.// We want the match function to return a string and to accept a number const resolver = createMatch<string, number>([ // Match arm which catches the values 0 or 1 (_: 0 | 1) => "not many", // Match arm which catches any number less than 9 (_: Max<9>) => "a few", // Match arm which catches any number that hasn't already been caught (_: number) => "lots" ]); // Transpiles to: const resolver = value_1 => { if (typeof value_1 === "number") { if (value_1 === 1 || value_1 === 0) return "not many"; else if (value_1 < 9) return "a few"; else return "lots"; } };
-
Equtility marker which compares a value to an expression, skipping validation of the value type.
Changes
Min,Max,Float, andIntutility markers now combine with thenumbertype by default, so doingnumber & Min<3>for example is excessive.Matchesutility marker now combines with thestringtype by default.
Bug fixes
- Unions that include the
booleantype no longer generate excessive code. - Random comments no longer get added to output (#26)