I am facing a situation where I need to pass an instance of an interface as an argument to a function that accepts any object type, thus lacking type checking. To ensure the object is of the correct type, I usually create an instance and then pass it:
const passMe: ITestInterface = { foo: "bar" };
someFunction(passMe);
However, I am looking for a more concise way to achieve this inline while still enforcing type checking.
// hypothetical syntax example
someFunction({ foo: "bar" } istype ITestInterface);
Is there a straightforward way to do something like the above example in one line?
I've experimented with using 'as', but unfortunately, it doesn't restrict the type. For instance, the following is considered valid:
someFunction({ foo: "bar", hello: true } as ITestInterface);
One workaround could be to modify the someFunction
to utilize templating, but this may not always be feasible or practical:
someFunction<TYPE>(arg: TYPE) {
// function body
}
someFunction<ITestInterface>({foo: "bar"});