I am struggling to articulate this question properly, so please refer to the code below
interface TestParams<T> {
order?: keyof T
attr1?: number
attr2?: string
}
async function Test<T = any>(_obj: TestParams<T>): Promise<T> {
return {} as T
}
Test({ order: 'id2' })
// function Test<{
// id2: any;
// }>(_obj: TestParams<{
// id2: any;
// }>): Promise<{
// id2: any;
// }>
Why is the T
type { id2: any; }
instead of just any
Here is the type I actually need
function Test<any>(_obj: TestParams<any>): Promise<PostgrestResponse<any>>
Update
Update
I added U
which represents keyof T
, and then implemented my required changes
interface TestParams<T> {
order?: T
attr1?: number
attr2?: string
}
async function test<T = any, U = keyof T>(_obj: TestParams<T extends undefined ? string : U>): Promise<T> {
return {} as T
}
test({ order: 'id2' })
// The resulting type is what I needed
// function test<any>(_obj: TestParams<"id2">): Promise<any>
interface TestType {
id2: number
id3: number
}
test<TestType>({ order: 'id2' })