#Define an interface for interfaces

45 messages · Page 1 of 1 (latest)

fleet dock
#

lets say I have a Record where each item needs to hold {content:unknown}.
Is it now possible that I create a new interface foo which needs to fullfill the condition above.
something like

interface foo extends myCondition {
test:true // would cause an error
bar: {content:string} // valid
bar: {content:number} // valid
}
glacial trench
#

@fleet dock I think mostly "no".

fleet dock
glacial trench
#

Did you do this?

interface IndexSignature {
  [key: string]: { content: unknown }
}


interface SpecificInterface extends IndexSignature {
  foo: { content: "foo"}
  test: 0
}
#

This is why I said "mostly" - but I don't think this is a good idea.

fleet dock
#

OMG

#

In the exact second

#

I fixed the different issue

#

which was caused by this

#

LMAO

fleet dock
glacial trench
#

I don't think so. I guess this is okay if you're using --noUncheckedIndexedAccess.

fleet dock
#

nah its causing other issues

#

I have in some places keyof

#

and if i create a type extending that to check the schema

#

it will allow for any key

glacial trench
#

Yup, that's what the index signature does.

mystic inletBOT
#


interface SpecificInterface extends IndexSignature {
  foo: { content: "foo"}
}

declare const x: SpecificInterface;
const y = x.bar; // compiles
glacial trench
#

Though if you turn on noUncheckedIndexedAccess then at least x.bar will be { content: unknown } | undefined.

fleet dock
#

nah i bite the bullet

#

i just prepare a template to copy

#

I am creating an modular event system where people can define value types etc

#

so i wanted to write a small support

#

but its not worth it if it causes these issues

#

Deleting it in a short bit

#

the last thing i need to figure out is that if the type of extensionData is not an empty object that it then is required, otherwise optional

glacial trench
#

If you use types, they can be assignable to an index signature without needing to incldue it explicitly:

mystic inletBOT
#
interface IndexSignature {
  [key: string]: { content: unknown }
}
let i: IndexSignature;

type Type1 = {
  foo: { content: "foo"}
}

declare const x1: Type1;
i = x1; // compiles

type Type2 = {
  foo: { content: "foo"},
  bar: 0
}
declare const x2: Type2;

i = x2;
//
// Type 'Type2' is not assignable to type 'IndexSignature'.
//   Property 'bar' is incompatible with index signature.
//     Type 'number' is not assignable to type '{ content: unknown; }'.
fleet dock
#

@glacial trench issue is that here im moving purely in types, no const let etc

#

the type extension is to allow to define the types of the event system easier

#

it has no actual effect

glacial trench
#

Yeah, I don't think there's a way to enforce constraints on types that are defined.

#

Except maybe generics, I guess?

fleet dock
#

oh yeah

#

i can try that

#

does this look like a good dev api btw?

glacial trench
#

I don't think I have enough context on what you're trying to do, honestly.

fleet dock
#

i am creating an event system for a website builder

#

so you can hook into the editor ui

#

and check if the editor creates an element

#

and then for example trigger custom plugin events

#

for some reason this omits all other propeties besides extensionData

#

wreid

#

solved

fleet dock
#

!solved