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

Fix #35060 #35065

Merged
merged 3 commits into from Nov 15, 2019
Merged

Fix #35060 #35065

merged 3 commits into from Nov 15, 2019

Conversation

jack-williams
Copy link
Collaborator

Fixes #35060, specifically the bug for ro-tuple to ro-tuple.

Some debate over cases like:

const t3: readonly [1] = [1];
const t4: [] = t3; // Type 'readonly [1]' is missing the following properties from type '[]': pop, push, reverse, shift, and 6 more. [2740]

It might be nice to just report the length delta, but that is for a different day.

*/
function tryElaborateArrayLikeErrors(source: Type, target: Type, reportErrors: boolean): boolean {
if (isTupleLikeType(source)) {
const sourceTuple: TupleType | undefined = (source as TupleTypeReference).target;
if (sourceTuple && sourceTuple.readonly && isArrayOrTupleLikeType(target) &&
(!isReadonlyArrayType(target) || isTupleType(target) && !target.target.readonly)) {
!isReadonlyArrayType(target) && !(isTupleLikeType(target) && (target as TupleTypeReference).target.readonly)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tuple-like type just needs to have a 0 property, so this cast isn't safe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative is just to dumb down the error message and check if it's strictly a tuple type or array type that isn't readonly, only then giving the adjusted error message

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, was abit confused by the comment on isTupleLikeType.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this check give the right error message for the following?

interface MyTupleLike { readonly 0: string }

let x: MyTupleLike = [10000] as const;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To elaborate, I think we're getting close, but

isArrayOrTupleLikeType(target)

probably needs to be replaced with something like

(isArrayType(target) || isTupleType(target))

so get rid of the Like

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message is now:

Type 'readonly [10000]' is not assignable to type 'MyTupleLike'.
  Types of property '0' are incompatible.
    Type '10000' is not assignable to type 'string'. [2322]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other baseline changes that remove: ... pop, push, concat, and 16 more.. Is that reasonable?

@@ -2,30 +2,30 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): erro
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2741: Property '2' is missing in type 'StrNum' but required in type '[number, number, number]'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, number, number]': 2, pop, push, concat, and 16 more.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, number, number]'.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be nice here to look for pairs of tuple-like things and elaborate if the lengths do not match? (Maybe not for this PR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if there's a known length property, that's likely good enough

const at3: readonly number[] = [1];
const at4: [1] = at3;
~~~
!!! error TS2740: Type 'readonly number[]' is missing the following properties from type '[1]': 0, pop, push, reverse, and 4 more.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this one doesn't get the readonly error message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition for readonly arrays on the source only checked for arrays on the target - will change and look for tuples.

const at3: readonly number[] = [1];
const at4: [1] = at3;
~~~
!!! error TS4104: The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type '[1]'.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser This is the new message for the case you pointed out.

@DanielRosenwasser DanielRosenwasser merged commit 3bcea0d into microsoft:master Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing error message with incompatible readonly tuple types
2 participants