Allow method type guards
See original GitHub issueTypeScript Version: 2.0.3
Code
Why I can have function type guard:
function hasValue<T>(value: T | undefined): value is T { return value !== undefined; }
but not method type guard:
export class Maybe<T> {
constructor(public value: T | undefined) {}
hasValue(): this.value is T { return this.value !== undefined; }
// ^ '{' or ';' expected.
}
?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:14
- Comments:10 (6 by maintainers)
Top Results From Across the Web
How to use type guards in TypeScript - LogRocket Blog
A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block.
Read more >5 Methods to use Type Guards in TypeScript - Bits and Pieces
Type Guards come into the picture with the usage of TypeScript. This technique is used to identify or narrow down the type of...
Read more >Documentation - Advanced Types - TypeScript
A type guard is some expression that performs a runtime check that guarantees the type in some scope. Using type predicates. To define...
Read more >How to get the types you want with TypeScript type guards
The TypeScript Handbook describes type guards as: Some expression that performs a runtime check that guarantees the type in some scope. The key ......
Read more >How To Do Anything in TypeScript With Type Guards
Type guards are conditional checks that allow types to be narrowed from general types to more specific ones. With type guards, we do ......
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
Semi related to a random thought here in that more complicated guards could express codependency of properties:
What about using
this
type guards instead?@mattmazzola I’ve used the above technique to describe a way that
Map#has
could work similarly, but it can get a bit messy.