Is it possible to restrict param not to accept strings, arrays etc.?
interface foo {
a?: number;
b?: string;
}
function baz(param: foo) {
}
baz("hello");
Is it possible to restrict param not to accept strings, arrays etc.?
interface foo {
a?: number;
b?: string;
}
function baz(param: foo) {
}
baz("hello");
You can do something like this to make baz accept at least an object:
interface foo {
a?: number;
b?: string;
}
interface notAnArray {
forEach?: void
}
type fooType = foo & object & notAnArray;
function baz(param: fooType) {
}
baz("hello"); // Throws error
baz([]); // Throws error
fooType here is an Intersection Type.
function baz(param: foo & object)object type is very new, you'll need TypeScript 2.2 to use it.interface NotAnArray { forEach?: void }