Skip to content

Not able to infer type by checking existence of property in object #38842

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
egonm12 opened this issue May 29, 2020 · 2 comments
Closed

Not able to infer type by checking existence of property in object #38842

egonm12 opened this issue May 29, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@egonm12
Copy link

egonm12 commented May 29, 2020

TypeScript Version: 3.9.2

Search Terms:
object type inference
incorrect type inference

Expected behavior:
When I have two interfaces that extend from the same base interface I should be able to check for the existence of a property within a variable that only exists in one of the two interfaces. TypeScript will not give an error saying that the property doesn't exist on the other type when I first check for its existence.

Actual behavior:
When I have two interfaces that extend from the same base interface I am not able to check for the existence of a property within a variable that only exists in one of the two interfaces. TypeScript will give an error saying that the property doesn't exist on the other type.

Related Issues:

Code

export type ProductDescription =
  | BagDescription
  | BeltDescription
  | ClothingDescription
  | ShoeDescription;

export interface BaseDescription {
  brand: string;
  name: string;
  partner_catalogue_number: number;
  color: string;
}

export type BagDescription = BaseDescription;

export interface BeltDescription extends BaseDescription {
  length: string;
  length_manufacturer: string;
}

export interface ClothingDescription extends BaseDescription {
  clothing_size_eu: string;
  clothing_size_manufacturer: string;
}

export interface ShoeDescription extends BaseDescription {
  shoe_size_eu: string;
  shoe_size_manufacturer: string;
}

// a response from an API request
// unkown what description will be
const description: ProductDescription = {} as ProductDescription;

// ts error while I expect this to work
// Property 'shoe_size_eu' does not exist on type 'ProductDescription'.
//  Property 'shoe_size_eu' does not exist on type 'BagDescription'
if (description.shoe_size_eu) console.log(description.shoe_size_eu)

// not a ts error
if ("shoe_size_eu" in description) console.log(description.shoe_size_eu)
Output
// a response from an API request
// unkown what ProductDescription will be
const description = {};
if (description.shoe_size_eu)
    console.log(description.shoe_size_eu);
if ("shoe_size_eu" in description)
    console.log(description.shoe_size_eu);
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 29, 2020
@RyanCavanaugh
Copy link
Member

This is the intended behavior; see #34590, #37343, or others

@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

No branches or pull requests

3 participants