Repository navigation
Type inference lost when destructuring #37638
Copy link
Copy link
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
MartinJohns commentedon Mar 27, 2020
Type narrowing via local variables is not supported. #12184 is not exactly the same, but similar enough.
jordyvandomselaar commentedon Mar 27, 2020
I see it has been marked as duplicate. I'm sorry for polluting your issues. Should I close it?
typescript-bot commentedon Mar 30, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.