-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Make type guard function types invariant in the type guarded for. #27686
Conversation
So we have a break in @typescript-bot test this |
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 1ac1528. You can monitor the build here. It should now contribute to this PR's status checks. |
RWC breaks which were all from OSS projects: mobx /* 'var' fixes small-build issue */
export var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap) as (
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thing: any
~~~~~~~~~~~~~~
) => thing is ObservableMap<any>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Conversion of type '(x: any) => x is ObservableMap<{}>' to type '(thing: any) => thing is ObservableMap<any>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type predicate 'x is ObservableMap<{}>' is not assignable to 'thing is ObservableMap<any>'. rxjs10 hits on this problem: // if the first and only other argument besides the resultSelector is an array
// assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
if (observables.length === 1 && isArray(observables[0])) {
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((arg: any) => arg is any[]) | (<T>(x: any) => x is T[])' has no compatible call signatures.
observables = <Array<Observable<any>>>observables[0];
}
where export const isArray = Array.isArray || (<T>(x: any): x is T[] => x && typeof x.length === 'number'); vs code export function createMonacoBaseAPI(): typeof monaco {
return {
editor: undefined,
languages: undefined,
CancellationTokenSource: CancellationTokenSource,
Emitter: Emitter,
KeyCode: KeyCode,
KeyMod: KeyMod,
Position: Position,
Range: Range,
Selection: Selection,
SelectionDirection: SelectionDirection,
Severity: Severity,
Promise: TPromise,
~~~~~~~
!!! error TS2322: Type 'typeof TPromise' is not assignable to type 'typeof Promise'.
!!! error TS2322: Types of property 'is' are incompatible.
!!! error TS2322: Type '(value: any) => value is Thenable<any>' is not assignable to type '(value: any) => value is monaco.Thenable<any>'.
!!! error TS2322: Type predicate 'value is Thenable<any>' is not assignable to 'value is Thenable<any>'.
!!! related TS6500 vs/monaco.d.ts:62:15: The expected type comes from property 'Promise' which is declared here on type 'typeof monaco'
Uri: URI,
~~~
!!! error TS2322: Type 'typeof URI' is not assignable to type 'typeof Uri'.
!!! error TS2322: Types of property 'isUri' are incompatible.
!!! error TS2322: Type '(thing: any) => thing is URI' is not assignable to type '(thing: any) => thing is Uri'.
!!! error TS2322: Type predicate 'thing is URI' is not assignable to 'thing is Uri'.
!!! related TS6500 vs/monaco.d.ts:131:15: The expected type comes from property 'Uri' which is declared here on type 'typeof monaco'
Token: Token
};
} |
What's the conclusion? Should I be working on contributing fixes for all the RWC breaks? |
1ac1528
to
7702c41
Compare
We'll need to review this in a backlog slog or design meeting. |
@typescript-bot test this again! |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 7702c41. You can monitor the build here. It should now contribute to this PR's status checks. |
@RyanCavanaugh this branch needs a sync with master for an accurate rwc run 💓 |
@mattmccutchen if you can get merged with master + CI green we can do an RWC run and then hopefully merge. Thanks! |
- Fix one break in the compiler. - Type guards like `isNetworked(): this is (Networked & this)` have been obsolete in favor of `isNetworked(): this is Networked` ever since narrowing was enhanced to take an intersection in a370908, and the first form now causes a problem: a subclass fails to be a subtype of its superclass because `this` appears in a non-covariant position. So replace all occurrences of the first form with the second form in the test suite. Fixes microsoft#26981.
7702c41
to
07cf82a
Compare
Updated to master. For reasons I haven't investigated, the |
@mattmccutchen sorry to continue kicking this slowly down the line; we probably won't ship this in 3.4 at this point; but could you resync this branch so we can run this branch against DT on our new infra? |
@weswigham @mattmccutchen I pushed a new baseline for the conflict |
Well I broke it... |
I don't know why we didn't see this CI failure before; I haven't investigated what changed. Anyway, the problem is with this code: interface Crate<T> {
// ...
isPackedTight(): this is (this & {extraContents: T});
} The We're left with the invariant occurrence of One solution would be to use a one-sided type guard (#15048), if and when those are available. Another is to write: interface Crate<T> {
// ...
isPackedTight<U>(this: this & Crate<U>): this is {extraContents: U};
} Now the type predicate no longer refers to FWIW, I applied the second solution to fix the CI. Of course, the more important thing is what we find in the RWC and DefinitelyTyped tests. |
The Crate<T> class had a "this" type predicate that referenced the type parameter T, preventing T from being covariant, which caused undesired behavior elsewhere. Apply the workaround described here: microsoft#27686 (comment)
76d0ba5
to
8de6b18
Compare
@typescript-bot run dt & @typescript-bot test this |
Heya @weswigham, I've started to run the extended test suite on this PR at 8de6b18. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @weswigham, I've started to run the Definitely Typed test suite on this PR at 8de6b18. You can monitor the build here. It should now contribute to this PR's status checks. |
This seems to break |
This PR hasn't seen any activity for quite a while, so I'm going to close it to keep the number of open PRs manageable. Feel free to open a fresh PR or continue the discussion here. |
Fix one break in the compiler, and add a workaround to test case
thisPredicateFunctionQuickInfo01, which used a code pattern that no
longer works.
Fixes #26981.