Skip to content

Literal inference causes unexpected errors on a values that trivially satisfy compound type intersections containing anyΒ #59473

Closed
@jedwards1211

Description

@jedwards1211

πŸ”Ž Search Terms

object intersection literal inference any

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about intersections and inference

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.4#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wChSB6CuGAC2AGc41ckntc4UA7OCAGwAmcAG5IoDYBF4RMcALIBVAJKkYATzBI4AIQgAPAAo4wTALxwA3qThwQ-APwAuLt3VwqcZXEHBB3fHgiFH5gAC9tOkYaTW0AdxQmHnVSAF9yTwAVeiZBCDYA+AYAVzQ0NgZMYv5+dwgxKCg-SNptMBNxDRitV2FvNB44TGB9OGAYADpKah1i+DptcRwoOwqUAHMW6IHihiRhBKYWbirJbnXeuG4kfaZQdvqkECRuGDVYhXVjCFM4Cz0jCYmAAyKw2OyOFz4fabfBwAA+V2KIAARuI0uRjgx4PJ1ABhXBgP5wAAUD1MLlx31MAEo-gA+JE1cgaHqZYlUoEAbXw9nwAF0PNRkpjpNi4KMLAAeXEE8AQswAIhhSEVHkZWViTFkcHJnXcvP4cJQRDG3BY4BQMGAKP4SCmtk1PXw2Ka5zh0W4EHgiUk624KFtkQg3W0+G4yLRK0RysEmzViOK3EESGG10E+AmJIATABmbPZmkZagAdXoaFoYzuMActmUTAABvYXMkG3wVtFxnAGxHUeIEXBY-GB0mU2n9g2HEA

πŸ’» Code

EDIT: simplified examples:

const a: [any] & [1] = [1] // Type '[number]' is not assignable to type '[1]'
const b: { ml: any } & { ml: 'edge' } = { ml: 'edge' } // Type '{ ml: string; }' is not assignable to type '{ ml: any; } & { ml: "edge"; }'

Original example:

import React from 'react'

// this comes from an old version of MUI
type BoxProps = {
  ml?: any // I didn't realize this type was any
}

// This doesn't successfully override the property type and I can fix it.
// But the error message this caused was nonsensical and needs improvement
type MyProps = BoxProps & {
  ml?: 'edge' | number
}

const MyComp = (props: MyProps) => null

type T = MyProps['ml'] // any

const x = <MyComp ml="edge" /> // Types of property 'ml' are incompatible.
  // Type 'string' is not assignable to type 'number | "edge" | undefined'.(2322)

// Which is it?  Is `ml: any` or is it `number | "edge" | undefined`?

πŸ™ Actual behavior

Type '{ ml: string; }' is not assignable to type '{ ml?: number | "edge" | undefined; }'.
  Types of property 'ml' are incompatible.
    Type 'string' is not assignable to type 'number | "edge" | undefined'.(2322)

πŸ™‚ Expected behavior

No error and no difference in the reported type for the ml property in JSX and MyProps['ml']

Additional information about the issue

I hope that literal inference can be improved to handle this case without an error:

const x: { ml: any } & { ml: 'edge' } = { ml: 'edge' }

Two potential solutions I proposed below are:

  1. Run a separate literal inference on each half of the required intersection type, then intersect the inferred types
  2. Fall back to unwidened type if it satisfies the required type but literal inference doesn't

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions