Assertion functions don't work when defined as methods
See original GitHub issueTypeScript Version:
3.9.0-dev.20200220
Search Terms:
assertion signature
Code
type Constructor<T> = new (...args: any[]) => T
let str: any = 'foo'
str.toUpperCase() // str is any
function assert(condition: unknown, message: string = 'Assertion failure', ErrorConstructor: Constructor<Error> = AssertionError): asserts condition {
if(!condition) {
throw new ErrorConstructor(message);
}
}
assert(typeof str == 'string')
str.toUpperCase() // str is string
class AssertionError extends Error { }
class Assertion {
assert(condition: unknown, message: string = 'Assertion failure', ErrorConstructor: Constructor<Error> = AssertionError): asserts condition {
if(condition) {
throw new ErrorConstructor(message);
}
}
}
let azzert = new Assertion().assert
let str2: any = 'bar'
azzert(typeof str2 == 'string') //error 2775
str2.toUpperCase() // str2 is any
Expected behavior:
Assertion functions are usable as methods of a class
Actual behavior:
Assertions require every name in the call target to be declared with an explicit type annotation.(2775) ‘azzert’ needs an explicit type annotation.
Playground Link:
Related Issues:
Perhaps this a design limitation per the following PR?
https://github.com/microsoft/TypeScript/pull/33622 https://github.com/microsoft/TypeScript/pull/33622#issuecomment-575301357
Issue Analytics
- State:
- Created 4 years ago
- Reactions:29
- Comments:29 (1 by maintainers)
Top Results From Across the Web
Why can't we use assertion for public methods? - Stack Overflow
An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or...
Read more >Assertion functions in TypeScript - LogRocket Blog
Let's explore assertion functions in TypeScript and see how they can be used to express invariants on our variables.
Read more >Programming With Assertions - Oracle Help Center
An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if...
Read more >Should I use assertions in a function that calls a function that ...
If G is a function which performs its business standalone and F doesn't care whether x > 0 or x <= 0, then...
Read more >Assertion Functions in TypeScript - Marius Schulz
TypeScript 3.7 implemented support for assertion functions in the type system. An assertion function is a function that throws an error if ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Assertion functions always need explicit annotations. You can write this:
Without being too repetitive, the following works for me: