Skip to content
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

fix: adds correct type inference when using .columns in SELECT #298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/ql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export declare class QL<T> {

export interface SELECT<T> extends Where<T>, And, Having<T>, GroupBy, OrderBy<T>, Limit {
// overload specific to SELECT
columns: Columns<T, SELECT<T>>['columns'] & ((projection: Projection<T>) => this)
columns: Columns<T, this>['columns'] & ((projection: Projection<T>) => this)
}
export class SELECT<T> extends ConstructedQuery<T> {
private constructor();
Expand Down
32 changes: 28 additions & 4 deletions test/typescript/apis/project/cds-ql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,37 @@ SELECT.from(Foos).columns(f => {
const number: QLExtensions<number> = f.x
})

SELECT.from(Foos).columns(['entityIDColumn', 'parentIDColumn'])
SELECT.from(Foos).columns('entityIDColumn', 'parentIDColumn')
SELECT.from(Foos).columns([{ ref: ['entityIDColumn'] }])
SELECT.from(Foos).columns({ ref: ['entityIDColumn'] })
// @ts-expect-error invalid key of result line
SELECT.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r[0].some)
SELECT.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r[0].ref)
// @ts-expect-error invalid key of result line
SELECT.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r[0].some)
SELECT.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r[0].ref)
// @ts-expect-error invalid key of result line
SELECT.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r[0].some)
SELECT.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r[0].ref)
// @ts-expect-error invalid key of result line
SELECT.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r[0].some)
SELECT.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r[0].ref)

SELECT.from `Books` .columns ( 'title', {ref:['author','name'],as:'author'} )
SELECT.from `Books` .columns (['title', {ref:['author','name'],as:'author'} ])

// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r.some)
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r.some)
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r.some)
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r.some)
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r.ref)

INSERT.into(Foos).values([1,2,3])

// tagged template strings
// literal
SELECT `a, b` .from `Books` .where `x = 42`
Expand Down