镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Type inference lost when destructuring #37638

Description

@jordyvandomselaar

TypeScript Version: Nightly

Search Terms:

  • Type inference wrong
  • Type destructuring

Expected behavior:

Type inference gets that my field is currently of dropdown type

Actual behavior:

It doesn't

Related Issues:

Code

export type DynamicFieldType = {
  visibleTo: "EVERYONE" | "EMPLOYEE" | "EMPLOYER";
  shouldBeVisibleTo: "EVERYONE" | "EMPLOYEE" | "EMPLOYER";
  name: string;
  isRequired: boolean;
  value: string;
  defaultValue: string;
  labels: {
    NL: string;
    EN: string;
  };
  onChange: (name: string, value: string) => void;
};

export type DropdownType = DynamicFieldType & {
  type: "dropdown";
  options: string[];
};

export type UploadType = DynamicFieldType & {
  type: "upload";
};

export type CheckboxType = DynamicFieldType & {
  type: "checkbox";
};

export type RadioType = DynamicFieldType & {
  type: "radio";
  options: string[];
};

export type TextType = DynamicFieldType & {
  type: "text";
};

export interface DynamicFieldProps {
  field: DropdownType | UploadType | CheckboxType | RadioType | TextType;
  intl: any; // TODO: update react-intl for types
}

const handleField = ({ field }: DynamicFieldProps): void => {
  switch (field.type) {
    case "dropdown":
      const foo = field.options;

      console.log("Success!");
  }
};

const failingHandleField = ({ field }: DynamicFieldProps): void => {
  const { type } = field;

  switch (type) {
    case "dropdown":
      const foo = field.options;

      console.log("It suddenly doesn't get it.");
  }
};
Output
const handleField = ({ field }) => {
    switch (field.type) {
        case "dropdown":
            const foo = field.options;
            console.log("Success!");
    }
};
const failingHandleField = ({ field }) => {
    const { type } = field;
    switch (type) {
        case "dropdown":
            const foo = field.options;
            console.log("It suddenly doesn't get it.");
    }
};
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

Activity

  1. MartinJohns commented on Mar 27, 2020

    @MartinJohns
    Contributor

    Type narrowing via local variables is not supported. #12184 is not exactly the same, but similar enough.

  2. jordyvandomselaar commented on Mar 27, 2020

    @jordyvandomselaar
    Author

    I see it has been marked as duplicate. I'm sorry for polluting your issues. Should I close it?

  3. typescript-bot commented on Mar 30, 2020

    @typescript-bot
    Contributor

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

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions