Search code examples
typescriptlodash

Import only the "get" function from lodash into typescript


How can I import only the get funtion from lodash.

I manged to import the complete lodash library by installing it and it's type. However I only need the get function. Is there a way?

My code (in angular but unrelated)

import { Component, OnInit } from '@angular/core';
import _ from "lodash";

@Component({
  selector: 'app-lodash-get',
  templateUrl: './lodash-get.component.html',
  styleUrls: ['./lodash-get.component.scss']
})
export class LodashGetComponent implements OnInit {

  readonly test = {
    level1: {
      level2: {
        level3: 'level3',
        array: [
          {
            data: "test"
          }
        ]
      }
    }
  };

  r1 = false;
  r2= false;
  _ : any = null;

  constructor() {
    this._ = _; 
   }

  ngOnInit() {
    this.r1 = _.get(this.test, 'level1.level2.level3', "No found");
    this.r2 = _.get(this.test, 'level1.level2.foo', "No found");
  }
}

Solution

  • mdn import states that you can import some functions from a module with brackets : import {get} from 'lodash';