Skip to content

Assignment to intersection type failing to check property type #13832

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

Closed
zamb3zi opened this issue Feb 2, 2017 · 2 comments
Closed

Assignment to intersection type failing to check property type #13832

zamb3zi opened this issue Feb 2, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@zamb3zi
Copy link

zamb3zi commented Feb 2, 2017

TypeScript Version: 2.1.5

Code

type A1 = {
    foo: number,
};
type B1 = A1 & {
    bar: {x: boolean}
};
const b1: B1 = {
    foo: 5,
    bar: {x: true, zoo: 5} // OK but expected: "'zoo' does not exist on type '{x: boolean}'"
}
b1.bar.zoo = 5; // Error as expected: "'zoo' does not exist on type '{x: boolean}'"
b1.bar = {x: true, zoo: 5}; // Error as expected: "'zoo' does not exist on type '{x: boolean}'"

type A2 = {
    foo: number,
    bar: {x: boolean} // this is the only difference from case 1 (type intersection is avoided)
};
const a2: A2 = {
    foo: 5,
    bar: {x: true, zoo: 5} // Error as expected: "'zoo' does not exist on type '{x: boolean}'"
}

Expected behavior:

When assigning to an intersection type, the types of the merged properties should be checked as if they were being assigned individually.

Actual behavior:

Type errors are missed, as described.

@zamb3zi
Copy link
Author

zamb3zi commented Feb 2, 2017

My workaround was to use classes instead. This works:

class C1 {
    foo: number;
}
class C2 extends C1 {
    bar: {x: boolean}
}
const c2: C2 = {
    foo: 5,
    bar: {x: true, zoo: 5} // Error as expected: "'zoo' does not exist on type '{x: boolean}'"
};

@sktw
Copy link

sktw commented Feb 2, 2017

I raised an issue about this yesterday #13813. Interesting that individual assignment errors are handled as expected.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 2, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants