Last active
August 26, 2019 22:21
-
-
Save krainboltgreene/aa20fda50d8aa581f2498743353cfe42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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