Reputation: 82396
I have the following TypeScript interface
interface ICookieOptions
{
name?:string;
value?:string;
secure?:boolean;
session?: boolean;
sameSite?: boolean;
}
and the following class
class Cookie
{
constructor(nameOrOptions: string | ICookieOptions)
{ [...]}
}
now I can do the following:
var testOptions =
{
name:"abc",
value:"def"
secure: false,
session: true,
sameSite: true
};
new Cookie(testOptions);
new Cookie(123);
and get no error.
I figured this is because all elements in ICookieOptions are optional.
And as soon as i remove the ? in name?, it outputs an error on the constructor with a number.
Now: How can I have ALL elements of ICookieOptions as optional, and restrict ICookieOptions to variables of type "object" (not number, boolean, etc.) ?
Upvotes: 0
Views: 255
Reputation: 82396
Ah, found the answer myselfs.
One needs to specify & object
in the type definition :
class Cookie
{
constructor(nameOrOptions: string | ICookieOptions & object)
{ [...]}
}
requires TypeScript v2.2+
Upvotes: 2