Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Last active August 26, 2019 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/aa20fda50d8aa581f2498743353cfe42 to your computer and use it in GitHub Desktop.
Save krainboltgreene/aa20fda50d8aa581f2498743353cfe42 to your computer and use it in GitHub Desktop.
get('hello')(new Map([['hello', 'world']]))
get('hello')({hello: 'world'})
get('hello')({goodbye: 'world'})
get(0)(['hello world'])
get(0)('hello world')
get(0)(null)
get(0)(undefined)
export default function get (name: unknown) {
return function getProperty (keyedFunctor: unknown): null | unknown {
if (keyedFunctor === undefined || keyedFunctor === null) {
return null;
}
if (typeof keyedFunctor === "object" && "get" in keyedFunctor && typeof keyedFunctor.get === "function") {
return keyedFunctor.get(name);
}
if (typeof keyedFunctor === "object" && (typeof name === "number" || typeof name === "string") && name in keyedFunctor) {
return keyedFunctor[name];
}
return null;
};
}
index.ts:9:90 - error TS2339: Property 'get' does not exist on type 'object'.
9 if (typeof keyedFunctor === "object" && "get" in keyedFunctor && typeof keyedFunctor.get === "function") {
~~~
index.ts:10:27 - error TS2339: Property 'get' does not exist on type 'object'.
10 return keyedFunctor.get(name);
~~~
index.ts:14:14 - error TS7053: Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
14 return keyedFunctor[name];
~~~~~~~~~~~~~~~~~~
Found 3 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment