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

Combination of intersections/partials causes invalid nested object type to not report error #51043

Closed
dlane-stripe opened this issue Oct 3, 2022 · 5 comments Β· Fixed by #51140
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@dlane-stripe
Copy link

Bug Report

πŸ”Ž Search Terms

partial nested object type

πŸ•— Version & Regression Information

  • This changed between versions 4.5.5 and 4.6.4. It reproduces in all versions since 4.6.4 (including nightly)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type OverridesInput = {
    someProp?: 'A' | 'B'
}

const foo: Partial<{something: any}> & {variables: {
    overrides?: OverridesInput;
} & Partial<{
    overrides?: OverridesInput;
}>} = {variables: {overrides: false}};

πŸ™ Actual behavior

There is no error on the overrides field in the object. Changing the type of foo slightly (removing one of the intersections) causes the error to appear correctly, but this unique combination of types produces no error. Does not matter what the contents of OverridesInput is, nor what the content inside the outer partial (currently {something: any}) is. Additionally, the following all produce the error correctly:

type OverridesInput = {
    someProp?: 'A' | 'B'
}

const foo: {variables: {
    overrides?: OverridesInput;
} & Partial<{
    overrides?: OverridesInput;
}>} = {variables: {overrides: false}};
type OverridesInput = {
    someProp?: 'A' | 'B'
}

const foo: Partial<{something: any}> & {variables: {
    overrides?: OverridesInput;
}} = {variables: {overrides: false}};
type OverridesInput = {
    someProp?: 'A' | 'B'
}

const foo: Partial<{something: any}> & {variables: Partial<{
    overrides?: OverridesInput;
}>} = {variables: {overrides: false}};

πŸ™‚ Expected behavior

There should be an error on the overrides field of the object since false is not assignable to OverridesInput | undefined.

@RyanCavanaugh
Copy link
Member

type OverridesInput = {
    someProp?: 'A' | 'B'
}

const foo: Partial<{something: any}> & {variables: {
    overrides?: OverridesInput;
} & Partial<{
    overrides?: OverridesInput;
// @ts-expect-error
}>} = {variables: {overrides: false}};

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Oct 4, 2022

This was a little easier for me to read.

interface Unrelated { _?: any }

// Identical but distinct types.
interface VariablesA { overrides?: OverridesInput; }
interface VariablesB { overrides?: OverridesInput; }

const foo: Unrelated & { variables: VariablesA & VariablesB } = {
    variables: {
        // Should be an error here:
        overrides: false
    }
};

I would've expected some weak type checking on this one.

@typescript-bot
Copy link
Collaborator

The change between release-4.4 and main occurred at 44e827b.

@RyanCavanaugh
Copy link
Member

@ahejlsberg thoughts? This changed in #47738

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Oct 5, 2022
@ahejlsberg
Copy link
Member

The issue here is that when the target in a relation is an intersection, common property checks are only performed at the top level. For example:

type A1 = { x: { a?: string } };
type B1 = { x: { b?: string } };
type C1 = { x: { c: string } };

const ab1: A1 & B1 = {} as C1;  // No error

type A2 = { a?: string };
type B2 = { b?: string };
type C2 = { c: string };

const ab2: A2 & B2 = {} as C2;  // Error

We definitely should error in both of the examples above.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 11, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 4.9.2 milestone Oct 11, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants