Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type predicate results stored in variables don't narrow guarded types #22341

Closed
bgmort opened this issue Mar 5, 2018 · 4 comments
Closed

Type predicate results stored in variables don't narrow guarded types #22341

bgmort opened this issue Mar 5, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@bgmort
Copy link

bgmort commented Mar 5, 2018

TypeScript Version: 2.8.0-dev.20180222

Search Terms:
assignable type guards
assignable type guard return value
assign type guard to variable

Code

interface A {
    type: 'A';
    aProperty: string;
}

interface B {
    type: 'B';
    bProperty: number;
}

type AorB = A | B;

function isA(obj: AorB): obj is A {
    return obj.type === 'A';
}

const o = { type: 'A', aProperty: 'value' } as any as AorB;

// this should be an error, and it is
console.log(o.aProperty);

// this should work, and it does
if (isA(o)) {
    console.log(o.aProperty);
}

// this example discards the type guard information, but it 
// ought to set oIsA to type (o is A), and apply the type
// guard inside the if statement. 
const oIsA = isA(o);
if (oIsA) {
    console.log(o.aProperty);
}

Expected behavior:
oIsA should have the type returned by the type guard function. if (oIsA) ... should narrow the type of o within the if block.

Actual behavior:
oIsA is a regular boolean value. The type guard function can only be used directly within a conditional block.

Playground Link:
https://goo.gl/6Pk5vr

Related Issues:

@DanielRosenwasser
Copy link
Member

I can't find the other duplicates, but we only check syntactically whether isA is a user-defined type guard at the if check (and other syntactic locations). The type predicate return type is really just a boolean in any other context. So oIsA doesn't track any information about the type of o itself.

We've considered this problem, but it seems fairly complex and expensive to solve in a general way.

@DanielRosenwasser DanielRosenwasser changed the title Assignable type guards Type predicate results stored in variables don't narrow guarded types Mar 5, 2018
@bgmort
Copy link
Author

bgmort commented Mar 6, 2018

@DanielRosenwasser I found the duplicates neither of us could find the first time around. This one seems to be the most similar: #14891

@mhegazy
Copy link
Contributor

mhegazy commented Mar 6, 2018

I think #14891 is a different issue..

the is a duplicate of #15482 #14630, #10976, #12263, #10065, and #10846

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 6, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants