Skip to content

typeof x === "y" type guard not working in strict mode #16949

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
iscekic opened this issue Jul 5, 2017 · 2 comments
Closed

typeof x === "y" type guard not working in strict mode #16949

iscekic opened this issue Jul 5, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@iscekic
Copy link

iscekic commented Jul 5, 2017

TypeScript Version: 2.4.0

Code

interface Repro {
  env: {
    [key: string]: string | undefined;
  };
}

const process: Repro = {
  env: {
    PORT: "3000"
  }
};

if (typeof process.env.PORT === "string") {
  parseInt(process.env.PORT);
}

Expected behavior:
The code is valid and passes compilation.

Actual behavior:
error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.

@RyanCavanaugh
Copy link
Member

Duplicate #10442 - we don't currently narrow except by declared properties, and there is no declared property named PORT

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 5, 2017
@Lord-Haji
Copy link

Lord-Haji commented Jul 5, 2017 via email

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

3 participants