-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Always discriminate contextual types by existing discriminant property #61097
Open
Andarist
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Andarist:fix/contextually-discriminate-on-existing-optional-property
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//// [tests/cases/compiler/contextuallyTypedByDiscriminableUnion2.ts] //// | ||
|
||
=== contextuallyTypedByDiscriminableUnion2.ts === | ||
type Props = | ||
>Props : Symbol(Props, Decl(contextuallyTypedByDiscriminableUnion2.ts, 0, 0)) | ||
|
||
| { | ||
parentId: string[]; | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 1, 5)) | ||
|
||
onChange: (event: { id: string }) => void; | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 2, 25)) | ||
>event : Symbol(event, Decl(contextuallyTypedByDiscriminableUnion2.ts, 3, 17)) | ||
>id : Symbol(id, Decl(contextuallyTypedByDiscriminableUnion2.ts, 3, 25)) | ||
|
||
onChange2: () => void; | ||
>onChange2 : Symbol(onChange2, Decl(contextuallyTypedByDiscriminableUnion2.ts, 3, 48)) | ||
} | ||
| { | ||
parentId?: never; | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 6, 5)) | ||
|
||
onChange: (event: { id: number }) => void; | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 7, 23)) | ||
>event : Symbol(event, Decl(contextuallyTypedByDiscriminableUnion2.ts, 8, 17)) | ||
>id : Symbol(id, Decl(contextuallyTypedByDiscriminableUnion2.ts, 8, 25)) | ||
|
||
}; | ||
|
||
function NonGenericComponent(props: Props) { | ||
>NonGenericComponent : Symbol(NonGenericComponent, Decl(contextuallyTypedByDiscriminableUnion2.ts, 9, 6)) | ||
>props : Symbol(props, Decl(contextuallyTypedByDiscriminableUnion2.ts, 11, 29)) | ||
>Props : Symbol(Props, Decl(contextuallyTypedByDiscriminableUnion2.ts, 0, 0)) | ||
|
||
return null; | ||
} | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent : Symbol(NonGenericComponent, Decl(contextuallyTypedByDiscriminableUnion2.ts, 9, 6)) | ||
|
||
onChange: (e) => {}, | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 15, 21)) | ||
>e : Symbol(e, Decl(contextuallyTypedByDiscriminableUnion2.ts, 16, 13)) | ||
|
||
}); | ||
|
||
const parentId: string[] = []; | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 19, 5)) | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent : Symbol(NonGenericComponent, Decl(contextuallyTypedByDiscriminableUnion2.ts, 9, 6)) | ||
|
||
parentId, | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 21, 21)) | ||
|
||
onChange: (e) => {}, | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 22, 11)) | ||
>e : Symbol(e, Decl(contextuallyTypedByDiscriminableUnion2.ts, 23, 13)) | ||
|
||
onChange2: () => {}, | ||
>onChange2 : Symbol(onChange2, Decl(contextuallyTypedByDiscriminableUnion2.ts, 23, 22)) | ||
|
||
}); | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent : Symbol(NonGenericComponent, Decl(contextuallyTypedByDiscriminableUnion2.ts, 9, 6)) | ||
|
||
parentId: parentId, | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 27, 21)) | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 19, 5)) | ||
|
||
onChange: (e) => {}, | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 28, 21)) | ||
>e : Symbol(e, Decl(contextuallyTypedByDiscriminableUnion2.ts, 29, 13)) | ||
|
||
onChange2: () => {}, | ||
>onChange2 : Symbol(onChange2, Decl(contextuallyTypedByDiscriminableUnion2.ts, 29, 22)) | ||
|
||
}); | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent : Symbol(NonGenericComponent, Decl(contextuallyTypedByDiscriminableUnion2.ts, 9, 6)) | ||
|
||
parentId: [], | ||
>parentId : Symbol(parentId, Decl(contextuallyTypedByDiscriminableUnion2.ts, 33, 21)) | ||
|
||
onChange: (e) => {}, | ||
>onChange : Symbol(onChange, Decl(contextuallyTypedByDiscriminableUnion2.ts, 34, 15)) | ||
>e : Symbol(e, Decl(contextuallyTypedByDiscriminableUnion2.ts, 35, 13)) | ||
|
||
onChange2: () => {}, | ||
>onChange2 : Symbol(onChange2, Decl(contextuallyTypedByDiscriminableUnion2.ts, 35, 22)) | ||
|
||
}); | ||
|
160 changes: 160 additions & 0 deletions
160
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
//// [tests/cases/compiler/contextuallyTypedByDiscriminableUnion2.ts] //// | ||
|
||
=== contextuallyTypedByDiscriminableUnion2.ts === | ||
type Props = | ||
>Props : Props | ||
> : ^^^^^ | ||
|
||
| { | ||
parentId: string[]; | ||
>parentId : string[] | ||
> : ^^^^^^^^ | ||
|
||
onChange: (event: { id: string }) => void; | ||
>onChange : (event: { id: string; }) => void | ||
> : ^ ^^ ^^^^^ | ||
>event : { id: string; } | ||
> : ^^^^^^ ^^^ | ||
>id : string | ||
> : ^^^^^^ | ||
|
||
onChange2: () => void; | ||
>onChange2 : () => void | ||
> : ^^^^^^ | ||
} | ||
| { | ||
parentId?: never; | ||
>parentId : undefined | ||
> : ^^^^^^^^^ | ||
|
||
onChange: (event: { id: number }) => void; | ||
>onChange : (event: { id: number; }) => void | ||
> : ^ ^^ ^^^^^ | ||
>event : { id: number; } | ||
> : ^^^^^^ ^^^ | ||
>id : number | ||
> : ^^^^^^ | ||
|
||
}; | ||
|
||
function NonGenericComponent(props: Props) { | ||
>NonGenericComponent : (props: Props) => null | ||
> : ^ ^^ ^^^^^^^^^ | ||
>props : Props | ||
> : ^^^^^ | ||
|
||
return null; | ||
} | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent({ onChange: (e) => {},}) : null | ||
> : ^^^^ | ||
>NonGenericComponent : (props: Props) => null | ||
> : ^ ^^ ^^^^^^^^^ | ||
>{ onChange: (e) => {},} : { onChange: (e: { id: number; }) => void; } | ||
> : ^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^ | ||
|
||
onChange: (e) => {}, | ||
>onChange : (e: { id: number; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>(e) => {} : (e: { id: number; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>e : { id: number; } | ||
> : ^^^^^^ ^^^ | ||
|
||
}); | ||
|
||
const parentId: string[] = []; | ||
>parentId : string[] | ||
> : ^^^^^^^^ | ||
>[] : never[] | ||
> : ^^^^^^^ | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent({ parentId, onChange: (e) => {}, onChange2: () => {},}) : null | ||
> : ^^^^ | ||
>NonGenericComponent : (props: Props) => null | ||
> : ^ ^^ ^^^^^^^^^ | ||
>{ parentId, onChange: (e) => {}, onChange2: () => {},} : { parentId: string[]; onChange: (e: { id: string; }) => void; onChange2: () => void; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
parentId, | ||
>parentId : string[] | ||
> : ^^^^^^^^ | ||
|
||
onChange: (e) => {}, | ||
>onChange : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>(e) => {} : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>e : { id: string; } | ||
> : ^^^^^^ ^^^ | ||
|
||
onChange2: () => {}, | ||
>onChange2 : () => void | ||
> : ^^^^^^^^^^ | ||
>() => {} : () => void | ||
> : ^^^^^^^^^^ | ||
|
||
}); | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent({ parentId: parentId, onChange: (e) => {}, onChange2: () => {},}) : null | ||
> : ^^^^ | ||
>NonGenericComponent : (props: Props) => null | ||
> : ^ ^^ ^^^^^^^^^ | ||
>{ parentId: parentId, onChange: (e) => {}, onChange2: () => {},} : { parentId: string[]; onChange: (e: { id: string; }) => void; onChange2: () => void; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
parentId: parentId, | ||
>parentId : string[] | ||
> : ^^^^^^^^ | ||
>parentId : string[] | ||
> : ^^^^^^^^ | ||
|
||
onChange: (e) => {}, | ||
>onChange : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>(e) => {} : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>e : { id: string; } | ||
> : ^^^^^^ ^^^ | ||
|
||
onChange2: () => {}, | ||
>onChange2 : () => void | ||
> : ^^^^^^^^^^ | ||
>() => {} : () => void | ||
> : ^^^^^^^^^^ | ||
|
||
}); | ||
|
||
NonGenericComponent({ | ||
>NonGenericComponent({ parentId: [], onChange: (e) => {}, onChange2: () => {},}) : null | ||
> : ^^^^ | ||
>NonGenericComponent : (props: Props) => null | ||
> : ^ ^^ ^^^^^^^^^ | ||
>{ parentId: [], onChange: (e) => {}, onChange2: () => {},} : { parentId: never[]; onChange: (e: { id: string; }) => void; onChange2: () => void; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
parentId: [], | ||
>parentId : never[] | ||
> : ^^^^^^^ | ||
>[] : never[] | ||
> : ^^^^^^^ | ||
|
||
onChange: (e) => {}, | ||
>onChange : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>(e) => {} : (e: { id: string; }) => void | ||
> : ^ ^^^^^^^^ ^^^^^^^^^^^^ | ||
>e : { id: string; } | ||
> : ^^^^^^ ^^^ | ||
|
||
onChange2: () => {}, | ||
>onChange2 : () => void | ||
> : ^^^^^^^^^^ | ||
>() => {} : () => void | ||
> : ^^^^^^^^^^ | ||
|
||
}); | ||
|
96 changes: 96 additions & 0 deletions
96
tests/baselines/reference/contextuallyTypedByDiscriminableUnion3.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//// [tests/cases/compiler/contextuallyTypedByDiscriminableUnion3.ts] //// | ||
|
||
=== contextuallyTypedByDiscriminableUnion3.ts === | ||
// https://github.com/microsoft/TypeScript/issues/58508 | ||
|
||
type PathSegment = object[]; | ||
>PathSegment : Symbol(PathSegment, Decl(contextuallyTypedByDiscriminableUnion3.ts, 0, 0)) | ||
|
||
type Handle<TData> = { | ||
>Handle : Symbol(Handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 2, 28)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 4, 12)) | ||
|
||
crumbBuilder: (data: TData) => PathSegment[]; | ||
>crumbBuilder : Symbol(crumbBuilder, Decl(contextuallyTypedByDiscriminableUnion3.ts, 4, 22)) | ||
>data : Symbol(data, Decl(contextuallyTypedByDiscriminableUnion3.ts, 5, 17)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 4, 12)) | ||
>PathSegment : Symbol(PathSegment, Decl(contextuallyTypedByDiscriminableUnion3.ts, 0, 0)) | ||
|
||
}; | ||
|
||
type Loader<TData> = (args: { | ||
>Loader : Symbol(Loader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 6, 2)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 12)) | ||
>args : Symbol(args, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 22)) | ||
|
||
params: Record<string, string>; | ||
>params : Symbol(params, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 29)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
}) => Promise<TData>; | ||
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 12)) | ||
|
||
type RouteHandler<TData = any> = | ||
>RouteHandler : Symbol(RouteHandler, Decl(contextuallyTypedByDiscriminableUnion3.ts, 10, 21)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 12, 18)) | ||
|
||
| { | ||
handle: Handle<never>; | ||
>handle : Symbol(handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 13, 5)) | ||
>Handle : Symbol(Handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 2, 28)) | ||
|
||
loader?: never; | ||
>loader : Symbol(loader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 14, 28)) | ||
} | ||
| { | ||
handle: Handle<TData>; | ||
>handle : Symbol(handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 17, 5)) | ||
>Handle : Symbol(Handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 2, 28)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 12, 18)) | ||
|
||
loader: Loader<TData>; | ||
>loader : Symbol(loader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 18, 28)) | ||
>Loader : Symbol(Loader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 6, 2)) | ||
>TData : Symbol(TData, Decl(contextuallyTypedByDiscriminableUnion3.ts, 12, 18)) | ||
|
||
}; | ||
|
||
const routeHandlerWithoutLoader = { | ||
>routeHandlerWithoutLoader : Symbol(routeHandlerWithoutLoader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 22, 5)) | ||
|
||
handle: { | ||
>handle : Symbol(handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 22, 35)) | ||
|
||
crumbBuilder: (data) => [], | ||
>crumbBuilder : Symbol(crumbBuilder, Decl(contextuallyTypedByDiscriminableUnion3.ts, 23, 11)) | ||
>data : Symbol(data, Decl(contextuallyTypedByDiscriminableUnion3.ts, 24, 19)) | ||
|
||
}, | ||
} satisfies RouteHandler; | ||
>RouteHandler : Symbol(RouteHandler, Decl(contextuallyTypedByDiscriminableUnion3.ts, 10, 21)) | ||
|
||
const routeHandler = { | ||
>routeHandler : Symbol(routeHandler, Decl(contextuallyTypedByDiscriminableUnion3.ts, 28, 5)) | ||
|
||
loader: async (args) => { | ||
>loader : Symbol(loader, Decl(contextuallyTypedByDiscriminableUnion3.ts, 28, 22)) | ||
>args : Symbol(args, Decl(contextuallyTypedByDiscriminableUnion3.ts, 29, 17)) | ||
|
||
return args.params.userId; | ||
>args.params : Symbol(params, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 29)) | ||
>args : Symbol(args, Decl(contextuallyTypedByDiscriminableUnion3.ts, 29, 17)) | ||
>params : Symbol(params, Decl(contextuallyTypedByDiscriminableUnion3.ts, 8, 29)) | ||
|
||
}, | ||
handle: { | ||
>handle : Symbol(handle, Decl(contextuallyTypedByDiscriminableUnion3.ts, 31, 4)) | ||
|
||
crumbBuilder: (data) => [], | ||
>crumbBuilder : Symbol(crumbBuilder, Decl(contextuallyTypedByDiscriminableUnion3.ts, 32, 11)) | ||
>data : Symbol(data, Decl(contextuallyTypedByDiscriminableUnion3.ts, 33, 19)) | ||
|
||
}, | ||
} satisfies RouteHandler<string>; | ||
>RouteHandler : Symbol(RouteHandler, Decl(contextuallyTypedByDiscriminableUnion3.ts, 10, 21)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it's at least moderately concerning that this leaves the code with only one use of
isPossiblyDiscriminantValue
; shouldn't it be the case that ifisPossiblyDiscriminantValue
returns false,isDiscriminantProperty
should have also returned false?The documentation for
isPossiblyDiscriminantValue
implies that it's there to avoid circularities, so I'm surprised that it is a problem, and that we aren't somehow introducing circularities by not calling it anymore...But even more surprisingly, if you just straight up delete
isPossiblyDiscriminantValue
and its one use (so, make it so that it always returns true), no test fails.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not be about circularities in the form of implicit any. One of the failures here is related to a code like this:
At the moment, the inner
ko.observable
has a resolving signature - that is temporarily set on it during inference. Requesting its type here changes that prematurely or smth like that. I haven't yet had time to investigate this further to see what I could do to mitigate this problem though.