[Typescript] Accept Anything Except Null or Undefined

Zhentiw發表於2024-07-04
const acceptAnythingExceptNullOrUndefined = <T>(input: {}) => {};

acceptAnythingExceptNullOrUndefined('hello');
acceptAnythingExceptNullOrUndefined(42);
acceptAnythingExceptNullOrUndefined(true);
acceptAnythingExceptNullOrUndefined(Symbol('foo'));
acceptAnythingExceptNullOrUndefined({});
acceptAnythingExceptNullOrUndefined([]);
acceptAnythingExceptNullOrUndefined(() => {});
acceptAnythingExceptNullOrUndefined(/foo/);
acceptAnythingExceptNullOrUndefined(new Error('foo'));

acceptAnythingExceptNullOrUndefined(
  // @ts-expect-error
  null
);
acceptAnythingExceptNullOrUndefined(
  // @ts-expect-error
  undefined
);

[Typescript] Accept Anything Except Null or Undefined

相關文章