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

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