Currently, I am working on a school project that requires me to develop a type-safe LINQ in Typescript using advanced types.
I am facing a challenge in figuring out how to ensure all my tables (types) can utilize the same interface.
My goal is to be able to chain functions like the following example:
students.Select("Name", "Surname").Include("Grades", q =>
q.Select("Grade", "CourseId" )
)
To start off, I defined a type Student
and created a table students
with some sample values:
type Student = {
Name: string,
Surname: string
}
let students: Student[] = [
{Name: "Foo", Surname: "Bar"},
{Name: "John", Surname: "Wick"}
]
I wanted to combine this setup with an interface to avoid repeating the Select
function multiple times.
interface Entity<T> {
Select: (args: T[]) => T
}
However, attempting to merge them using type Student = Entity<T> & {
does not seem to work as it would require adding a generic type T
to the students table and creating a separate Select
for each type.
How can I ensure that the students
table can leverage functions from Entity<T>
and chain them together as shown in the initial example? Or should I consider approaching the problem differently?
If more details are needed, I am happy to provide them without making this explanation too lengthy.