#Extended interface inside function argument

23 messages · Page 1 of 1 (latest)

timid socket
#

Is it possible to declare an interface with extends inside an argument?

Like:

function example(options: { ...stuff } extends BaseOptions) {
  // hello world
}
blazing quail
#

no, what are you trying to do exactly?

#

options: BaseOptions lets you pass anything extending BaseOptions just fine.

timid socket
#

im doing this right now

#

and in typedoc

blazing quail
#

what are you trying to do though?

timid socket
#

I dont want to declare an interface for opts

blazing quail
#

you don't need to

#

you could just intersect it into a new type, BaseOptions & { collection?: string; id: string }

#

it'll make documentation kinda worse though since those other props aren't documented

timid socket
#

oh damn i didnt know that's possible

blazing quail
#

!:thats%

turbid shardBOT
#
tjjfvi#0
`!t6:thats-no-generic-its-a-type-cast`:

You can use generics in objects, but usually they should be inferable from the structure of the object.

The classic example of a "misused generic" is a function like:

function getMeA<T>(): T {
   /* magic */
}

Since the generic has no relation to anything in the call signature, it can't be inferred and also there's no way to actually implement that function safely: it's not really a generic, it's a type-cast that's pretending to be a generic.

timid socket
#

How can I resolve that?

blazing quail
#

well, not using that pattern

timid socket
blazing quail
#

if you can't specify the result type statically, it's generally safer to have it be a broader type then specifically assert it where it's known

blazing quail
# timid socket

this doesn't really matter, it's more about what the function declares.

timid socket
#

I don't get it

timid socket
#

one step further but still

#

i prefer it be the entire thing