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 code raises a TS2345 from the compiler when attempting to use any property of a union that is not common to all types of the union. This only occurs when passing an object literal to a generic function which has inferred its generic type from contextual typing of the return type.
In this case; the language service does indicate that it has properly determined the correct type, but will not accept any uncommon property belong to only one type of the union.
π Expected behavior
In less complex contexts; this would work, and seems as though it should work here.
The text was updated successfully, but these errors were encountered:
T extends {profileId:string} was also a constraint on the input (as well as the output). When you Omit that constraint on the input it becomes 'no constraint', e.g., any. Replacing the input constraint with any
seems to make the problem go away.
function ensureProfileId<T extends { profileId: string }>(selector: any): T {
return { ...selector, ...{profileId: "2" } as T;
}
@craigphicks, unfortunately the input must be typed to modified generic type to ensure the correct object is being passed. The language service shows it determines the correct type. This may actually be an issue with Omit since writing a non-generic form of the ensureProfileId function products similar results AND will not itself compile.
Alright; after determining the issue is indeed with Omit and searching on that. What I found is that Omit and Pick do not distribute over unions and that this is working as intended per #28339
Creating and using a distributed form will fix this:
Bug Report
π Search Terms
TS2345
Object literal may only specify known properties
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The code raises a TS2345 from the compiler when attempting to use any property of a union that is not common to all types of the union. This only occurs when passing an object literal to a generic function which has inferred its generic type from contextual typing of the return type.
In this case; the language service does indicate that it has properly determined the correct type, but will not accept any uncommon property belong to only one type of the union.
π Expected behavior
In less complex contexts; this would work, and seems as though it should work here.
The text was updated successfully, but these errors were encountered: