Releases: GoogleFeud/ts-runtime-checks
Releases · GoogleFeud/ts-runtime-checks
v0.6.3
Additions
- Markers are now removed from typescript declaration files. For this to work, you must set the
afterDeclarations
plugin 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 withafterDeclarations
set 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
v0.6.1
v0.6.0
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
andNull
types which can be used with conditional transformations and checks - null & Transform<...> isnever
, so these types need to exist.PostCheck
utility type which can be used alongsideTransform
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.
v0.5.1
v0.5.0
Additions
-
Check
marker 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
Check
s. 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
,MinLen
andMaxLen
are now inclusive (#51)
v0.4.2
v0.4.1
v0.4.0
Additions
-
A
createMatch
utility 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"; } };
-
Eq
utility marker which compares a value to an expression, skipping validation of the value type.
Changes
Min
,Max
,Float
, andInt
utility markers now combine with thenumber
type by default, so doingnumber & Min<3>
for example is excessive.Matches
utility marker now combines with thestring
type by default.
Bug fixes
- Unions that include the
boolean
type no longer generate excessive code. - Random comments no longer get added to output (#26)