skip to Main Content

I understand below two usages in TS/ES6.

  1. Below, a variable myname of type string which is going to be constant(final) is declared.

    const myname: string = ‘abcd’;

  2. Below a variable/function initialPath is declared and is assigned a function body using the => operator.

    const initialPath = ({pathRoot, myvar = ”, locale = ”}) =>
    path.join(process.cwd(), pathRoot, myvar, locale.toLowerCase());

But I am not sure of the below usage, what is : in the below doing.

module.exports = {
  application: myname,
  myservicePostfix,
  loadablePath: ({pathRoot, myvar}) =>
    path.join(basePath({pathRoot, myvar}), 'loadable.json')
}

-> does application: myname mean that application = myname and application is of type typeof(myname).
-> similarily what does loadablePath: above mean

2

Answers


  1. The thing your confused about is simply a dictionary object. Here is another example of an object like yours:

    const object = {
        thing1: “hello”,
        thing2: 78,
        thing3: () => {console.log(“thing3 has been run!”)},
        thing4: [6, 3, 4, 1, 8]
    }
    

    As you can see, my object variable contains four elements, or things. Each of them can be called by writing object.nameOfThing.

    In your instance, you have a nested function as an element, the one that is called loadablePath. That is very much the same as my thing3 element, which runs its function whenever it’s called:

    object.thing3()
    // result —-> thing3 has been run!
    

    In simple words, yes. Just as object.thing1: “hello” means that object.thing1 = “hello”, application: myname means that application = myname.

    This is simply a dictionary object and that’s as far as it goes. Hope this helps.

    Login or Signup to reply.
  2. There’s no TypeScript involved in your example. I get that some confusion may arise from the fact that TS uses colons to associate a type with a variable, but none of that is in play here. This is pure javascript.

    In a javascript object literal, properties are declared with key: value:

    const myObj = {
      foo: "bar",
      num: 42,
      myField: true
    }
    

    This is equivalent to:

    const myObj = {};
    myObj.foo = "bar";
    myObj.num = 42;
    myObj.myField = true;
    

    The value can be any valid javascript type, including a function:

    function myFunction () {
      console.log('my function invoked');
    }
    
    const someObject = {
      someFunction: myFunction
    }
    
    someObject.someFunction(); // logs "my function invoked"

    You can also use an arrow function:

    const myFunction = () => console.log('my function invoked');
    
    const someObject = {
      someFunction: myFunction
    }
    
    someObject.someFunction(); // logs "my function invoked"

    The arrow function above can be inlined in the object declaration:

    const someObject = {
      someFunction: () => console.log('my function invoked')
    }
    
    someObject.someFunction(); // logs "my function invoked"

    Putting this all together using your example:

    // mocking these out because loadablePath references them
    const path = { join: (...args) => args.join('/')}
    const basePath = ({ pathRoot, myvar }) => path.join(pathRoot, myvar);
    
    // declare the string constants referenced in your example
    const myname = "Appy McApperson";
    const myservicePostfix = 'My Service Postfix';
    
    // module has to exist before we can assign it an exports property
    const module = {};
    
    
    module.exports = {
      application: myname,
      myservicePostfix,
      loadablePath: ({pathRoot, myvar}) =>
        path.join(basePath({pathRoot, myvar}), 'loadable.json')
    }
    
    
    console.log(module.exports.application)
    // "Appy McApperson"
    
    
    console.log(module.exports.myservicePostfix)
    // "My Service Postfix"
    
    
    console.log(module.exports.loadablePath({
      pathRoot: '/path/root',
      myvar: 'somepath'
    })); 
    // "/path/root/somepath/loadable.json"
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search