Skip to content

Type guard does not work with object properties #6238

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

Closed
tkrotoff opened this issue Dec 24, 2015 · 1 comment
Closed

Type guard does not work with object properties #6238

tkrotoff opened this issue Dec 24, 2015 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@tkrotoff
Copy link

This works:

let strOrArray: Array<string> | string;
let str: string;
if (typeof strOrArray === 'string') {
  // No error
  str = strOrArray;
}

But not this:

interface Data {
  strOrArray: Array<string> | string;
}
let data: Data;
let str: string;
if (typeof data.strOrArray === 'string') {
  // Error:
  // Type 'string[] | string' is not assignable to type 'string'.
  // Type 'string[]' is not assignable to type 'string'.
  str = data.strOrArray;
}

typeguard

Tested with tsc 1.7.5 and Visual Studio Code 0.10.6

@ahejlsberg
Copy link
Member

This is a duplicate of #3812.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Dec 25, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

2 participants