I would like to create an interface for an object that has a certain type for a specific named property and a different type for all other properties.
How would I write a definition for foo
below?
let foo = {
size: 3,
a: 'foo',
b: 'bar',
c: 'baz'
}
This would be my intuitive approach:
interface Foo {
size: number;
[name: string]: string;
}
However, TypeScript tries to apply the general definition to the specific definition and triggers the following error:
error TS2411: Property 'size' of type 'number' is not assignable to string index type 'string'.
You must tell TypeScript if a property is optional. First, if you don't tell TypeScript that a property is optional, it will expect it to be set. Adding ? to the property name on a type, interface, or class definition will mark that property as optional.
Use the Omit utility type to override the type of an interface property, e.g. interface SpecificLocation extends Omit<Location, 'address'> {address: newType} . The Omit utility type constructs a new type by removing the specified keys from the existing type.
// One major difference between type aliases vs interfaces are that interfaces are open and type aliases are closed. This means you can extend an interface by declaring it a second time. // In the other case a type cannot be changed outside of its declaration.
TypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. To create an interface, use the interface keyword followed by the interface name and the typed object.
You can do something like this (I don't know if it will work in TS v1.5):
interface Size {
size: number;
}
interface StringMap {
[name: string]: string;
}
type Foo = Size & StringMap;
Without defining every property, that's expected, but you can do this:
interface Foo1 {
size: number;
[name: string]: string|number;
}
Better than using any
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With