You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+70
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,7 @@ There are some options to configure the transformer.
137
137
|`ignoreFunctions`*(deprecated, use `functionBehavior` instead)*| Boolean (default: `false`). If `true`, when the transformer encounters a function, it will ignore it and simply return `true`. If `false`, an error is generated at compile time. |
138
138
|`functionBehavior`| One of `error`, `ignore`, or `basic` (default: `error`). Determines the behavior of transformer when encountering a function. `error` will cause a compile-time error, `ignore` will cause the validation function to always return `true`, and `basic` will do a simple function-type-check. Overrides `ignoreFunctions`. |
139
139
|`disallowSuperfluousObjectProperties`| Boolean (default: `false`). If `true`, objects are checked for having superfluous properties and will cause the validation to fail if they do. If `false`, no check for superfluous properties is made. |
140
+
|`transformNonNullExpressions`| Boolean (default: `false`). If `true`, non-null expressions (eg. `foo!.bar`) are checked to not be `null` or `undefined`|
140
141
|`emitDetailedErrors`| Boolean or `auto` (default: `auto`). The generated validation functions can return detailed error messages, pointing out where and why validation failed. These messages are used by `assertType<T>()`, but are ignored by `is<T>()`. If `false`, validation functions return empty error messages, decreasing code size. `auto` will generate detailed error messages for assertions, but not for type checks. `true` will always generate detailed error messages, matching the behaviour of version 0.18.3 and older. |
141
142
142
143
If you are using `ttypescript`, you can include the options in your `tsconfig.json`:
@@ -152,6 +153,7 @@ If you are using `ttypescript`, you can include the options in your `tsconfig.js
152
153
"ignoreMethods": true,
153
154
"functionBehavior": "ignore",
154
155
"disallowSuperfluousObjectProperties": true,
156
+
"transformNonNullExpressions": true,
155
157
"emitDetailedErrors": "auto"
156
158
}
157
159
]
@@ -259,6 +261,74 @@ new A().method(42) === 42; // true
259
261
newA().method('42'asany); // will throw error
260
262
```
261
263
264
+
### async and `Promise` returning methods
265
+
`AssertType` can also work correctly with `async` methods, returning promise rejected with `TypeGuardError`
266
+
267
+
To enable this functionality, you need to emit decorators metadata for your TypeScript project.
268
+
269
+
```json
270
+
{
271
+
"compilerOptions": {
272
+
"emitDecoratorMetadata": true
273
+
}
274
+
}
275
+
```
276
+
277
+
Then `AssertType` will work with async methods and `Promise` returning methods automatically.
This family of functions check not only whether the passed object is assignable to the specified type, but also checks that the passed object does not contain any more than is necessary. In other words: the type is also "assignable" to the object. This functionality is equivalent to specifying `disallowSuperfluousObjectProperties` in the options, the difference is that this will apply only to the specific function call. For example:
0 commit comments