Skip to content

Use full isReadonlySymbol check rather than declaration flags #48064

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

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19747,9 +19747,10 @@ namespace ts {
// This ensures the subtype relationship is ordered, and preventing declaration order
// from deciding which type "wins" in union subtype reduction.
// They're still assignable to one another, since `readonly` doesn't affect assignability.
// This is only applied during the strictSubtypeRelation -- currently used in subtype reduction
Copy link
Member

Choose a reason for hiding this comment

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

It would probably be better to include which operations will use this, something like:

Suggested change
// This is only applied during the strictSubtypeRelation for compatability's sake in control flow
// This is only applied during the strictSubtypeRelation -- currently used in subtype reduction

if (
(relation === subtypeRelation || relation === strictSubtypeRelation) &&
!!(sourcePropFlags & ModifierFlags.Readonly) && !(targetPropFlags & ModifierFlags.Readonly)
relation === strictSubtypeRelation &&
isReadonlySymbol(sourceProp) && !isReadonlySymbol(targetProp)
) {
return Ternary.False;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/typeGuardNarrowByMutableUntypedField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [typeGuardNarrowByMutableUntypedField.ts]
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { [K in P]: unknown };
declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
let x: number = arrayLikeOrIterable.length;
}

//// [typeGuardNarrowByMutableUntypedField.js]
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
var x = arrayLikeOrIterable.length;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/typeGuardNarrowByMutableUntypedField.ts ===
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { [K in P]: unknown };
>hasOwnProperty : Symbol(hasOwnProperty, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 0))
>P : Symbol(P, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 32))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>target : Symbol(target, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 55))
>property : Symbol(property, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 66))
>P : Symbol(P, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 32))
>target : Symbol(target, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 55))
>K : Symbol(K, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 94))
>P : Symbol(P, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 32))

declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByMutableUntypedField.ts, 1, 13))
>ArrayLike : Symbol(ArrayLike, Decl(lib.es5.d.ts, --, --))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))

if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
>hasOwnProperty : Symbol(hasOwnProperty, Decl(typeGuardNarrowByMutableUntypedField.ts, 0, 0))
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByMutableUntypedField.ts, 1, 13))

let x: number = arrayLikeOrIterable.length;
>x : Symbol(x, Decl(typeGuardNarrowByMutableUntypedField.ts, 3, 7))
>arrayLikeOrIterable.length : Symbol(ArrayLike.length, Decl(lib.es5.d.ts, --, --))
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByMutableUntypedField.ts, 1, 13))
>length : Symbol(ArrayLike.length, Decl(lib.es5.d.ts, --, --))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/typeGuardNarrowByMutableUntypedField.ts ===
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { [K in P]: unknown };
>hasOwnProperty : <P extends PropertyKey>(target: {}, property: P) => target is { [K in P]: unknown; }
>target : {}
>property : P

declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
>arrayLikeOrIterable : ArrayLike<any> | Iterable<any>

if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
>hasOwnProperty(arrayLikeOrIterable, 'length') : boolean
>hasOwnProperty : <P extends PropertyKey>(target: {}, property: P) => target is { [K in P]: unknown; }
>arrayLikeOrIterable : ArrayLike<any> | Iterable<any>
>'length' : "length"

let x: number = arrayLikeOrIterable.length;
>x : number
>arrayLikeOrIterable.length : number
>arrayLikeOrIterable : ArrayLike<any>
>length : number
}
11 changes: 11 additions & 0 deletions tests/baselines/reference/typeGuardNarrowByUntypedField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [typeGuardNarrowByUntypedField.ts]
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { readonly [K in P]: unknown };
declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
let x: number = arrayLikeOrIterable.length;
}

//// [typeGuardNarrowByUntypedField.js]
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
var x = arrayLikeOrIterable.length;
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/typeGuardNarrowByUntypedField.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/typeGuardNarrowByUntypedField.ts ===
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { readonly [K in P]: unknown };
>hasOwnProperty : Symbol(hasOwnProperty, Decl(typeGuardNarrowByUntypedField.ts, 0, 0))
>P : Symbol(P, Decl(typeGuardNarrowByUntypedField.ts, 0, 32))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>target : Symbol(target, Decl(typeGuardNarrowByUntypedField.ts, 0, 55))
>property : Symbol(property, Decl(typeGuardNarrowByUntypedField.ts, 0, 66))
>P : Symbol(P, Decl(typeGuardNarrowByUntypedField.ts, 0, 32))
>target : Symbol(target, Decl(typeGuardNarrowByUntypedField.ts, 0, 55))
>K : Symbol(K, Decl(typeGuardNarrowByUntypedField.ts, 0, 103))
>P : Symbol(P, Decl(typeGuardNarrowByUntypedField.ts, 0, 32))

declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByUntypedField.ts, 1, 13))
>ArrayLike : Symbol(ArrayLike, Decl(lib.es5.d.ts, --, --))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))

if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
>hasOwnProperty : Symbol(hasOwnProperty, Decl(typeGuardNarrowByUntypedField.ts, 0, 0))
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByUntypedField.ts, 1, 13))

let x: number = arrayLikeOrIterable.length;
>x : Symbol(x, Decl(typeGuardNarrowByUntypedField.ts, 3, 7))
>arrayLikeOrIterable.length : Symbol(ArrayLike.length, Decl(lib.es5.d.ts, --, --))
>arrayLikeOrIterable : Symbol(arrayLikeOrIterable, Decl(typeGuardNarrowByUntypedField.ts, 1, 13))
>length : Symbol(ArrayLike.length, Decl(lib.es5.d.ts, --, --))
}
21 changes: 21 additions & 0 deletions tests/baselines/reference/typeGuardNarrowByUntypedField.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/typeGuardNarrowByUntypedField.ts ===
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { readonly [K in P]: unknown };
>hasOwnProperty : <P extends PropertyKey>(target: {}, property: P) => target is { readonly [K in P]: unknown; }
>target : {}
>property : P

declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
>arrayLikeOrIterable : ArrayLike<any> | Iterable<any>

if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
>hasOwnProperty(arrayLikeOrIterable, 'length') : boolean
>hasOwnProperty : <P extends PropertyKey>(target: {}, property: P) => target is { readonly [K in P]: unknown; }
>arrayLikeOrIterable : ArrayLike<any> | Iterable<any>
>'length' : "length"

let x: number = arrayLikeOrIterable.length;
>x : number
>arrayLikeOrIterable.length : number
>arrayLikeOrIterable : ArrayLike<any>
>length : number
}
6 changes: 6 additions & 0 deletions tests/cases/compiler/typeGuardNarrowByMutableUntypedField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @lib: es6
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { [K in P]: unknown };
declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
let x: number = arrayLikeOrIterable.length;
}
6 changes: 6 additions & 0 deletions tests/cases/compiler/typeGuardNarrowByUntypedField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @lib: es6
declare function hasOwnProperty<P extends PropertyKey>(target: {}, property: P): target is { readonly [K in P]: unknown };
declare const arrayLikeOrIterable: ArrayLike<any> | Iterable<any>;
if (hasOwnProperty(arrayLikeOrIterable, 'length')) {
let x: number = arrayLikeOrIterable.length;
}