Is it possible to have a conditional type that can test for objects that can be empty?
For example:
function test<T>(a: T): T extends {} ? string : never {
return null
}
let o1: {}
let o2: { fox? }
let o3: { fox }
test(o1)
test(o2)
test(o3) // this one should be 'never'
Since the conditional type test also for inheritance, all 3 cases yield 'string' but I want to yield 'never' if at least one property of the type is required (non empty object type like o3
)
UPDATE
When I wrote this question, I tried to nail down the cause of a problem I was having. I though I should solve my doubt not my problem and also simplify my question. However the answers deviate from my problem.
Basically I was trying to build a function where the first argument is an object and the second one is optional if the first can be fully partial (initialized with {})
function test<T extends {}>(a: T, ...x: T extends {} ? [never?] : [any])
function test(a, b) {
return null
}
let o1: {}
let o2: { fox? }
let o3: { fox }
test(o1) // ok
test(o2) // ok
test(o3) // should fail and require second argument