You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following snippet emits an error in TS 3.4 whereas in TS 3.3 it compiles fine.
classErrorResult{readonlyisError=true;staticcombine(errorResults: ReadonlyArray<ErrorResult>): ErrorResult{returnnewErrorResult(/* ... combine errorResults ... */);}}//functionextractAndCombineErrorResults<T,TErrorResultextendsErrorResult>(allResults: ReadonlyArray<T|TErrorResult>,combine: (details: ReadonlyArray<TErrorResult>)=>TErrorResult,): ReadonlyArray<T>|TErrorResult{return/* dummy result: */[];}//interfaceSomeResult{isSomeResult: true}constallResults: ReadonlyArray<SomeResult|ErrorResult>=[];// Here the inference doesn't work anymore:constresult1: ReadonlyArray<SomeResult>|ErrorResult=extractAndCombineErrorResults(allResults,ErrorResult.combine);// Explicit generic args work:constresult2: ReadonlyArray<SomeResult>|ErrorResult=extractAndCombineErrorResults<SomeResult,ErrorResult>(allResults,ErrorResult.combine);
Command line: npx tsc TypeScriptIssue.ts
Expected behavior:
No compile error.
Actual behavior:
TSC emits
TypeScriptIssue30074.ts:28:7 - error TS2322: Type 'ErrorResult | readonly (ErrorResult | SomeResult)[]' is not assignable to type 'ErrorResult | readonly SomeResult[]'.
Type 'readonly (ErrorResult | SomeResult)[]' is not assignable to type 'ErrorResult | readonly SomeResult[]'.
Type 'readonly (ErrorResult | SomeResult)[]' is not assignable to type 'readonly SomeResult[]'.
Type 'ErrorResult | SomeResult' is not assignable to type 'SomeResult'.
Property 'isSomeResult' is missing in type 'ErrorResult' but required in type 'SomeResult'.
28 const result1: ReadonlyArray<SomeResult> | ErrorResult =
~~~~~~~
This is working as intended and is an effect of #29847. In your example, we now end up making inferences from the allResults parameter which we previously wouldn't do. Therefore, in the result1 assignment we now infer SomeResult | ErrorResult for T, and we'll make that inference even if you omit the type annotation for result1 (where previously we'd then infer {} for T).
ah, now I see that in TS 3.3 the inference comes from the result assignment (without, it inferred ReadonlyArray<SomeResult | ErrorResult> | ErrorResult).
Many thanks for your reply!
I've now found an alternative solution to make inference work again (based on the second combine argument, which fixates TErrorResult) using Exclude<T, TErrorResult> for the result.
TypeScript Version: 3.4.0-dev.20190223
Search Terms: 3.4 inference generic readonly
Code
The following snippet emits an error in TS 3.4 whereas in TS 3.3 it compiles fine.
Command line:
npx tsc TypeScriptIssue.ts
Expected behavior:
No compile error.
Actual behavior:
TSC emits
Playground Link: (works in TS 3.3)
Possibly related: #29435
The text was updated successfully, but these errors were encountered: