Coder Social home page Coder Social logo

ruslauz / get-key-value Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexandermckay/get-key-value

0.0 0.0 0.0 183 KB

Avoid "Element implicitly has an 'any' type because expression of type 'string' can't be used to index" errors

JavaScript 20.02% TypeScript 79.98%

get-key-value's Introduction

Get Key Value

Size

~ 38 bytes once minified

NPM

https://www.npmjs.com/package/get-key-value

Installation

npm install get-key-value

yarn add get-key-value

Purpose

If you have been using Typescript you have likely stumbled into the Element implicitly has an 'any' type because expression of type 'string' can't be used to index error.

This tiny package makes it easy to deal with this error.

Explanation of the Warning

const user = {name: 'Bill'}

const getKeyValueA = (key: string, obj: object) => obj[key]
getKeyValueA('name', user) // Won't work

const getKeyValueB = (key: string, obj: Record<string, any>) =>
  obj[key]
getKeyValue('name', user) // Will work but at the loss of type safety

const getKeyValue = <U extends keyof T, T extends object>(
  key: U,
  obj: T
) => obj[key]
getKeyValue<keyof typeof user, typeof user>('name', user) // Will work with type safety

getKeyValueA

  • We set the key parameter to be type string
  • We set the obj parameter to be type object
  • The object type defaults to an empty object {}
  • The are no string in an empty object, therefore a string cannot be used to retrieve a value

getKeyValueB

  • We set the key parameter to be type string
  • We set the obj parameter to be type Record<string, any>
// Valid Record<string, any>
const user = {
  name: 'Bill',
  age: 20,
  likes: ['turtles', 'rock climbing'],
}
// Invalid Record<string, any>
const user = {1: 'Bill', 2: 20, 3: ['turtles', 'rock climbing']}
  • Whilst this works, we lose typesafety as any string can be used to index an object and the type of the value will be any

getKeyValue

  • We set the key parameter to be the generic U which is a keyof T
  • We set the obj parameter to be the generic T which extends object
  • T extends an empty object and U extends the keys of T. Therefore U will always exist on T and can be used as a look up value.

Usage

There are two ways to use the getKeyValue function:

Option A

Use this option if you have a interface defined for your object

interface User {
  name: string
  age: number
}
const user: User = {name: 'John Smith', age: 20}
getKeyValue<keyof User, User>('name', user) // => 'John Smith'

Option B

Use this option if you don't have an interface defined for your object

const user = {name: 'John Smith', age: 20}
getKeyValue<keyof typeof user, typeof user>('name', user) // => 'John Smith'

get-key-value's People

Contributors

alexandermckay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.