Scipion
Scipion

Reputation: 11888

Creating a Type with a minimal set of properties

I would like to create the type Foo with as minimal properties needed blah by instance :

interface Foo {
    blah: string;
}

f: Foo = {blah: "lol"}

What I want though, is that an error is mentioned if the property blah isn't specified but also that an error isn't mentioned if a Foo has more properties than blah

f: Foo = {boo: "lol"} // property blah is missing

f: Foo = {blah: "lol", boo: "lol"} // this is fine

So to sum it up, having the properties I mention in my interface, as a minimal set of properties.

How can I achieve that (it doesn't work with my Foo interface as mentioned above).

Upvotes: 0

Views: 364

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164307

Will this do:

interface Foo {
    blah: string;
    [name: string]: string;
}

let f1: Foo = {boo: "lol"} // error
let f2: Foo = {blah: "lol", boo: "lol"} // fine

Upvotes: 2

Related Questions