Skip to content

Exclude special index signature rule from strict subtype relation #53388

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 4 commits into from
Mar 23, 2023
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22429,7 +22429,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const targetHasStringIndex = some(indexInfos, info => info.keyType === stringType);
let result = Ternary.True;
for (const targetInfo of indexInfos) {
const related = !sourceIsPrimitive && targetHasStringIndex && targetInfo.type.flags & TypeFlags.Any ? Ternary.True :
const related = relation !== strictSubtypeRelation && !sourceIsPrimitive && targetHasStringIndex && targetInfo.type.flags & TypeFlags.Any ? Ternary.True :
isGenericMappedType(source) && targetHasStringIndex ? isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, RecursionFlags.Both, reportErrors) :
typeRelatedToIndexInfo(source, targetInfo, reportErrors, intersectionState);
if (!related) {
Expand Down
155 changes: 155 additions & 0 deletions tests/baselines/reference/narrowingMutualSubtypes.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
tests/cases/compiler/narrowingMutualSubtypes.ts(117,17): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
tests/cases/compiler/narrowingMutualSubtypes.ts(118,29): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.


==== tests/cases/compiler/narrowingMutualSubtypes.ts (2 errors) ====
// Check that `any` is a strict supertype of `unknown`

declare const ru1: { [x: string]: unknown };
declare const ra1: { [x: string]: any };

const a1a = [ru1, ra1]; // { [x: string]: any }[]
const a1b = [ra1, ru1]; // { [x: string]: any }[]

declare const ra2: { [x: string]: any };
declare const ru2: { [x: string]: unknown };

const a2a = [ru2, ra2]; // { [x: string]: any }[]
const a2b = [ra2, ru2]; // { [x: string]: any }[]

// Check that `{}` is strict supertype of any non-empty object

const c3 = {};
declare const r3: { [x: string]: unknown }

const a3a = [c3, r3]; // {}[]
const a3b = [r3, c3]; // {}[]

declare const r4: { [x: string]: unknown }
const c4 = {};

const a4a = [c4, r4]; // {}[]
const a4b = [r4, c4]; // {}[]

// Check that {} is a strict supertype of Record<string, unknown>

declare function isObject1(value: unknown): value is Record<string, unknown>;

function gg1(x: {}) {
if (isObject1(x)) {
x; // Record<string, unknown>
}
else {
x; // {}
}
x; // {}
}

declare function isObject2(value: unknown): value is {};

function gg2(x: Record<string, unknown>) {
if (isObject2(x)) {
x; // Record<string, unknown>
}
else {
x; // never
}
x; // Record<string, unknown>
}

// Check that {} is a strict supertype of Record<string, any>

declare function isObject3(value: unknown): value is Record<string, any>;

function gg3(x: {}) {
if (isObject3(x)) {
x; // Record<string, any>
}
else {
x; // {}
}
x; // {}
}

declare function isObject4(value: unknown): value is {};

function gg4(x: Record<string, any>) {
if (isObject4(x)) {
x; // Record<string, any>
}
else {
x; // never
}
x; // Record<string, any>
}

// Repro from #50916

type Identity<T> = {[K in keyof T]: T[K]};

type Self<T> = T extends unknown ? Identity<T> : never;

function is<T>(value: T): value is Self<T> {
return true;
}

type Union = {a: number} | {b: number} | {c: number};

function example(x: Union) {
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
if (is(x)) {}
x; // Union
}

function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
// "accidentally" guards the first branch on the length
if (Array.isArray(obj) && obj.length) {
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
else {
// 'obj' should probably not include an array type here.
for (let key in obj) {
if (obj[key] !== undefined) {
~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
console.log(obj[key])
~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
}
}
}
}

function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
if (Array.isArray(obj)) {
// obj should only be an array type here
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
else {
// 'obj' should probably not include an array type here.
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
}

132 changes: 125 additions & 7 deletions tests/baselines/reference/narrowingMutualSubtypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const c4 = {};
const a4a = [c4, r4]; // {}[]
const a4b = [r4, c4]; // {}[]

// Check that narrowing preserves original type in false branch for non-identical mutual subtypes
// Check that {} is a strict supertype of Record<string, unknown>

declare function isObject1(value: unknown): value is Record<string, unknown>;

function gg(x: {}) {
function gg1(x: {}) {
if (isObject1(x)) {
x; // Record<string, unknown>
}
Expand All @@ -45,14 +45,40 @@ declare function isObject2(value: unknown): value is {};

function gg2(x: Record<string, unknown>) {
if (isObject2(x)) {
x; // {}
x; // Record<string, unknown>
}
else {
x; // Record<string, unknown>
x; // never
}
x; // Record<string, unknown>
}

// Check that {} is a strict supertype of Record<string, any>

declare function isObject3(value: unknown): value is Record<string, any>;

function gg3(x: {}) {
if (isObject3(x)) {
x; // Record<string, any>
}
else {
x; // {}
}
x; // {}
}

declare function isObject4(value: unknown): value is {};

function gg4(x: Record<string, any>) {
if (isObject4(x)) {
x; // Record<string, any>
}
else {
x; // never
}
x; // Record<string, any>
}

// Repro from #50916

type Identity<T> = {[K in keyof T]: T[K]};
Expand All @@ -76,6 +102,44 @@ function example(x: Union) {
if (is(x)) {}
x; // Union
}

function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
// "accidentally" guards the first branch on the length
if (Array.isArray(obj) && obj.length) {
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
else {
// 'obj' should probably not include an array type here.
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
}

function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
if (Array.isArray(obj)) {
// obj should only be an array type here
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
else {
// 'obj' should probably not include an array type here.
for (let key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key])
}
}
}
}


//// [narrowingMutualSubtypes.js]
Expand All @@ -92,7 +156,7 @@ var a3b = [r3, c3]; // {}[]
var c4 = {};
var a4a = [c4, r4]; // {}[]
var a4b = [r4, c4]; // {}[]
function gg(x) {
function gg1(x) {
if (isObject1(x)) {
x; // Record<string, unknown>
}
Expand All @@ -103,13 +167,31 @@ function gg(x) {
}
function gg2(x) {
if (isObject2(x)) {
x; // {}
x; // Record<string, unknown>
}
else {
x; // Record<string, unknown>
x; // never
}
x; // Record<string, unknown>
}
function gg3(x) {
if (isObject3(x)) {
x; // Record<string, any>
}
else {
x; // {}
}
x; // {}
}
function gg4(x) {
if (isObject4(x)) {
x; // Record<string, any>
}
else {
x; // never
}
x; // Record<string, any>
}
function is(value) {
return true;
}
Expand All @@ -124,3 +206,39 @@ function example(x) {
if (is(x)) { }
x; // Union
}
function checksArrayOrObject1(obj) {
// "accidentally" guards the first branch on the length
if (Array.isArray(obj) && obj.length) {
for (var key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key]);
}
}
}
else {
// 'obj' should probably not include an array type here.
for (var key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key]);
}
}
}
}
function checksArrayOrObject2(obj) {
if (Array.isArray(obj)) {
// obj should only be an array type here
for (var key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key]);
}
}
}
else {
// 'obj' should probably not include an array type here.
for (var key in obj) {
if (obj[key] !== undefined) {
console.log(obj[key]);
}
}
}
}
Loading