#Type correctly function as value or proxy

5 messages · Page 1 of 1 (latest)

steel orbit
#

I have a function which I'm unable to correctly type somehow it won't let me type both return options at the same time correctly. The function can return a proxy or a function call which should represent a value.

function example<T>(arg: T): (() => T) | T {
  if (arg && typeof arg === "object") {
    return new Proxy<T & object>(arg as T & object, {
      get(target, p, reciver) {
        return hasKey(target, p) && target[p];
      },
    });
  }

  return function () {
    return arg as any;
  };
}


// EXAMPLE
const somenumber = count(0);
const user = example({ username: "fluix", age: 33 });

/*
This expression is not callable.
  Not all constituents of type '0 | (() => 0)' are callable.
    Type '0' has no call signatures.
*/
count();

// autocomplete does not work
user.age;
``` As you can see both give me an error the first TS should know it is a value, and the second it should know the properties of the object suggest.
inland nymph
#

!hb overload

#

!hb function overloads

#

These aren't good links 😒