#Make generic optional by-default and required otherwise
1 messages · Page 1 of 1 (latest)
Preview:```ts
interface BarOptions<Data = void> {
count: number
data: Data
}
class Foo<Data = void> {
bar(options: BarOptions<Data>) {}
}
const one = new Foo<string>()
one.bar({count: 10, data: "hello"})
const two = new Foo()
two.bar({count: 10}) // shouldn't error```
You can choose specific lines to embed by selecting them before copying the link.
Make generic optional by-default and required otherwise
something like this?
Preview:```ts
type BarOptions<Data> = {
count: number
} & ([Data] extends [never] ? {} : {data: Data})
class Foo<Data = never> {
bar(options: BarOptions<Data>) {}
}
const one = new Foo<string>()
one.bar({count: 10, data: "hello"})
const two = new Foo()
...```
You can choose specific lines to embed by selecting them before copying the link.
Seems to work great, thank you. How do I "move" the data now when it's uncertain if it exists?