Skip to content

Array of objects with same fields but mapped to different enum types causes "Error: Debug Failure. False expression". #7629

Closed
@markwongsk

Description

@markwongsk

TypeScript Version:

1.8.7

Code

file1.ts:

export enum MyEnum {
    FOO
};

export var object1 = {
    foo: MyEnum.FOO
};

file2.ts:

export enum MyEnum {
    BAR
}

export var object2 = {
    foo: MyEnum.BAR
};

file3.ts

import * as file1 from "./file1";
import * as file2 from "./file2";

let broken = [
    file1.object1,
    file2.object2
];

Run tsc file1.ts file2.ts file3.ts
Expected behavior:
Either compiler emits an error saying that it cannot find a common type for broken, or compilation succeeds.

Actual behavior:

/usr/local/lib/node_modules/typescript/lib/tsc.js:32996
                throw e;
                ^

Error: Debug Failure. False expression: 
    at Object.assert (/usr/local/lib/node_modules/typescript/lib/tsc.js:757:23)
    at reportError (/usr/local/lib/node_modules/typescript/lib/tsc.js:15894:26)
    at enumRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:16444:29)
    at isRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15922:34)
    at propertiesRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:16256:43)
    at objectTypeRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:16178:30)
    at isRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15991:38)
    at checkTypeRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15882:26)
    at checkTypeSubtypeOf (/usr/local/lib/node_modules/typescript/lib/tsc.js:15757:20)
    at isTypeSubtypeOf (/usr/local/lib/node_modules/typescript/lib/tsc.js:15751:20)

Note that casting the array explicitly, eg:

let broken: any[] = [
    file1.object1,
    file2.object2
];

let broken: any = [
    file1.object1,
    file2.object2
];

let broken = [
    file1.object1,
    file2.object2
] as any;

let broken = <any[]>[
    file1.object1,
    file2.object2
];

all fail, but casting either object1 or object2 to any would make compilation succeed. For example,

let broken = [
    file1.object1 as any,
    file2.object2
];

I'm guessing this is because the compiler does a subtype checking after it figures out the array type (which it fails to).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions