Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== tests/cases/compiler/controlFlowFavorAssertedTypeThroughTypePredicate.ts ===
// repro 49988#issuecomment-1192016929

declare function isObject(value: unknown): value is Record<string, unknown>;
>isObject : Symbol(isObject, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 0, 0))
>value : Symbol(value, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 2, 26))
>value : Symbol(value, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 2, 26))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

declare const obj1: {};
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))

obj1; // {}
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))

if (isObject(obj1)) {
>isObject : Symbol(isObject, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 0, 0))
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))

obj1; // Record<string, unknown>
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))

obj1['attr'];
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))
}
obj1; // Record<string, unknown>
>obj1 : Symbol(obj1, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 4, 13))

declare const obj2: {} | undefined;
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))

obj2; // {} | undefined
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))

if (isObject(obj2)) {
>isObject : Symbol(isObject, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 0, 0))
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))

obj2; // Record<string, unknown>
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))

obj2['attr'];
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))
}
obj2; // Record<string, unknown> | undefined
>obj2 : Symbol(obj2, Decl(controlFlowFavorAssertedTypeThroughTypePredicate.ts, 12, 13))

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/controlFlowFavorAssertedTypeThroughTypePredicate.ts ===
// repro 49988#issuecomment-1192016929

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

declare const obj1: {};
>obj1 : {}

obj1; // {}
>obj1 : {}

if (isObject(obj1)) {
>isObject(obj1) : boolean
>isObject : (value: unknown) => value is Record<string, unknown>
>obj1 : {}

obj1; // Record<string, unknown>
>obj1 : Record<string, unknown>

obj1['attr'];
>obj1['attr'] : unknown
>obj1 : Record<string, unknown>
>'attr' : "attr"
}
obj1; // Record<string, unknown>
>obj1 : Record<string, unknown>

declare const obj2: {} | undefined;
>obj2 : {} | undefined

obj2; // {} | undefined
>obj2 : {} | undefined

if (isObject(obj2)) {
>isObject(obj2) : boolean
>isObject : (value: unknown) => value is Record<string, unknown>
>obj2 : {} | undefined

obj2; // Record<string, unknown>
>obj2 : Record<string, unknown>

obj2['attr'];
>obj2['attr'] : unknown
>obj2 : Record<string, unknown>
>'attr' : "attr"
}
obj2; // Record<string, unknown> | undefined
>obj2 : Record<string, unknown> | undefined

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @noEmit: true
// @strict: true

// repro 49988#issuecomment-1192016929

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

declare const obj1: {};
obj1; // {}
if (isObject(obj1)) {
obj1; // Record<string, unknown>
obj1['attr'];
}
obj1; // Record<string, unknown>

declare const obj2: {} | undefined;
obj2; // {} | undefined
if (isObject(obj2)) {
obj2; // Record<string, unknown>
obj2['attr'];
}
obj2; // Record<string, unknown> | undefined
Copy link
Member

Choose a reason for hiding this comment

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

I know it's unfair to ask this of you since there were no tests, but:

  • I would remove the comments - the type baselines and errors will indicate these better and not need to be updated.
  • I would also add test cases for a type predicate like value is {} acting on Record<string, unknown> and Record<string, unknown> | undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem - I've added the requested test cases and removed those comments (I've originally kept them as they are pretty popular in your test suite)

Copy link
Member

@DanielRosenwasser DanielRosenwasser Jul 29, 2022

Choose a reason for hiding this comment

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

I've originally kept them as they are pretty popular in your test suite

Yeah, I am not a fan 😅

They fall out of date with the baselines so it never made sense.