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

Separate Variable Type Check Doesn't Guard Type #37855

Closed
fishcharlie opened this issue Apr 9, 2020 · 2 comments · Fixed by #44730
Closed

Separate Variable Type Check Doesn't Guard Type #37855

fishcharlie opened this issue Apr 9, 2020 · 2 comments · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@fishcharlie
Copy link
Contributor

TypeScript Version: 3.8.3

Search Terms:

  • separate variable type check guard
  • variable type check guard
  • variable type guard

Expected behavior:

No errors to be thrown.

Actual behavior:

When using the variable to check the type, the type of item and itemB respectively doesn't get narrowed down to the correct type. This works in the first example and it narrows down the type.

This encourages bad practice especially if you are having to repeat the type check over and over. In order to keep code from not becoming repetitive storing that in a variable seems like a much better practice.

Related Issues:

None

Code

class User {
    name?: string;
}

type Item = User | string;
const item: Item = Math.random() < .5 ? "Hello World" : new User();

let isItemUser = (item instanceof User);
console.log((item instanceof User) ? item.name : item); // types correct
console.log((isItemUser) ? item.name : item); // types incorrect


//////////////////////////////////////////////////////////////////////////


type ItemB = number | string;
const itemB: ItemB = Math.random() < .5 ? "Hello World" : 2;

let isItemBNumber = (typeof itemB === "number");
console.log((typeof itemB === "number") ? itemB * 100 : itemB); // types correct
console.log((isItemBNumber) ? itemB * 100 : itemB); // types incorrect
Output
"use strict";
class User {
}
const item = Math.random() < .5 ? "Hello World" : new User();
let isItemUser = (item instanceof User);
console.log((item instanceof User) ? item.name : item); // types correct
console.log((isItemUser) ? item.name : item); // types incorrect
const itemB = Math.random() < .5 ? "Hello World" : 2;
let isItemBNumber = (typeof itemB === "number");
console.log((typeof itemB === "number") ? itemB * 100 : itemB); // types correct
console.log((isItemBNumber) ? itemB * 100 : itemB); // types incorrect
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@MartinJohns
Copy link
Contributor

Duplicate of #12184.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Apr 10, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants