[Typescript] Accept Anything Except Null or Undefined

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
);

 

 

posted @ 2024-07-04 02:14  Zhentiw  阅读(5)  评论(0编辑  收藏  举报