question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow method type guards

See original GitHub issue

TypeScript 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:open
  • Created 7 years ago
  • Reactions:14
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
kitsonkcommented, Sep 24, 2016

Semi related to a random thought here in that more complicated guards could express codependency of properties:

interface Array<T> {
    length: number;
    pop(): this.length > 0 ? T : undefined;
}

const arr = [ 1, 2, 3 ];

if (arr.length > 0) {
    const val = arr.pop(); // val is number, not number | undefined under strictNullChecks
}
3reactions
DanielRosenwassercommented, Jul 12, 2017

What about using this type guards instead?

export class Maybe<T> {
    constructor(public value: T | undefined) {}

    hasValue(): this is this & { value: T } { return this.value !== undefined; }
}

///////

declare var x: Maybe<string>;
if (x.hasValue()) {
    x.value.toLowerCase();
}

@mattmazzola I’ve used the above technique to describe a way that Map#has could work similarly, but it can get a bit messy.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found