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

Type inference lost when destructuring #37638

Closed
jordyvandomselaar opened this issue Mar 27, 2020 · 3 comments · Fixed by #44730
Closed

Type inference lost when destructuring #37638

jordyvandomselaar opened this issue Mar 27, 2020 · 3 comments · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@jordyvandomselaar
Copy link

jordyvandomselaar commented Mar 27, 2020

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

@MartinJohns
Copy link
Contributor

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 27, 2020
@jordyvandomselaar
Copy link
Author

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

@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