π 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:
- Run a separate literal inference on each half of the required intersection type, then intersect the inferred types
- Fall back to unwidened type if it satisfies the required type but literal inference doesn't
π Search Terms
object intersection literal inference any
π Version & Regression Information
β― 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:
Original example:
π Actual behavior
π Expected behavior
No error and no difference in the reported type for the
mlproperty in JSX andMyProps['ml']Additional information about the issue
I hope that literal inference can be improved to handle this case without an error:
Two potential solutions I proposed below are: