Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

set-return-type.d.ts 1.7KB

1234567891011121314151617181920212223242526272829
  1. type IsAny<T> = 0 extends (1 & T) ? true : false; // https://stackoverflow.com/a/49928360/3406963
  2. type IsNever<T> = [T] extends [never] ? true : false;
  3. type IsUnknown<T> = IsNever<T> extends false ? T extends unknown ? unknown extends T ? IsAny<T> extends false ? true : false : false : false : false;
  4. /**
  5. Create a function type with a return type of your choice and the same parameters as the given function type.
  6. Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return `undefined` instead.
  7. @example
  8. ```
  9. import {SetReturnType} from 'type-fest';
  10. type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
  11. type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
  12. //=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
  13. ```
  14. */
  15. export type SetReturnType<Fn extends (...args: any[]) => any, TypeToReturn> =
  16. // Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
  17. Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (
  18. // If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
  19. // We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
  20. IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn
  21. ) : (
  22. // This part should be unreachable, but we make it meaningful just in case…
  23. (...args: Parameters<Fn>) => TypeToReturn
  24. );