Autocomplete breaks after one property for an array genericΒ #57879
Description
π Search Terms
array generic
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
β― Playground Link
π» Code
type ModelClause<T> = {
OR?: ModelClause<T>[];
AND?: ModelClause<T>[];
} & Partial<T>;
type BaseQuery<T extends string, U extends object> = {
model: T;
query?: ModelClause<U>;
};
type UserQuery = BaseQuery<"User", { name: string; age: number }>;
type PostsQuery = BaseQuery<"Posts", { title: string; content: string }>;
type Query = UserQuery | PostsQuery;
function testing<T extends Query>(query: T[]) {
console.log(query);
}
function testing2<T extends Query[]>(query: T) {
console.log(query);
}
testing([
{
model: "User",
query: {
name: "test",
// Typing age here will autocomplete
age: 10
},
},
]);
testing2([
{
model: "User",
query: {
name: "test",
// Try typing age here, it won't autocomplete
},
},
]);
π Actual behavior
For the testing
function, you correctly get suggested fields when filling in the query
property.
However, for testing2
, the suggestion works for your first property, whichever one you try.
However, once the first one is provided, it will stop autocompleting.
Once one has been provided, you can also list any key, regardless of if it's part of the model.
π Expected behavior
It should correctly suggest all properties and typecheck additional properties and not stop working after the first one.
Additional information about the issue
Some extra context why I'm using the testing2
approach, here's a playground link that shows what I'm trying to achieve.
Basically, I want to make sure that the return value of my function adheres to what was passed in. I haven't found a way to do it with testing
, so I went for testing2
, which is a decrease in DX since it'll only autocomplete one query parameter.
But the return value is exactly what I want
(for reference for others who would stumble upon this issue, the last as const
is important to ensure it returns the correct order, else it will have a type of const returnValue: (UserModel | PostModel)[]
)
Activity