question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nameof with arrays

See original GitHub issue

Hello!

I’m using ts-nameof of in conjuction with lodash _.set function to strongly type object paths in reactJS. I don’t know if this an error or a misuse of the package by me:

When I try to make something like this:

     var elem = {
            value: "Val",
            items: [
                {
                    prop1: 1,
                    prop2: "pepe"
                }]
        }

        console.log(nameof(elem.items[0].prop1));

I get this error:

Module build failed: Error: Invalid character in nameof argument: [ -- if this should be a valid character then please log an issue. If you wish, you can manually edit config.js in this package to allow the character.
    at throwInvalidCharacterInArg (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof\dist\handleNameOf.js:155:11)
    at tryGetArgs (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof\dist\handleNameOf.js:118:24)
    at Object.handleNameOf (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof\dist\handleNameOf.js:18:21)
    at NameOfFinder.indexOfAll (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof\dist\NameOfFinder.js:20:53)
    at Function.replaceInText (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof\dist\replaceInText.js:8:26)
    at Object.module.exports (D:\Documenta2\TLaw.Docu2\src\TLaw.Docu2.PublicWeb\node_modules\ts-nameof-loader\main.js:3:26)

I’m trying to get “elem.items[0].prop1”.

Any idea? THANKS

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Jun 17, 2017

@intnovaction that output seems correct to me. I don’t believe nameof should replace any identifiers with their constant values or current value in an iteration. Note that nameof is evaluated before the program is run so doing it in an iteration wouldn’t be possible. I think in this situation you would want to get the string and then add the value into it:

console.log(nameof.full(elem.items[index].prop1).replace(nameof(index), index));
// compiles to:
console.log("elem.items[index].prop1".replace("index", index));
1reaction
dsherretcommented, May 12, 2017

@intnovaction couldn’t think of a reason not to do this. This is released in 0.9.2. I increased the minor revision only because this is a very minor change with no api changes.

So yeah, just upgrade and use nameof.full(elem.items[0].prop1); and it should work well for you. Let me know if you have any other issues or find any problems and I’ll take a look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Is it possible to imply the name of the parameters of a ...
I thought I could make use of the new c# 6 operator nameof to build a dictionary of key/values implicitly from a params...
Read more >
Working With Arrays In C#
Array in C# represents a collection of items. C# Array class is used to create an array in C# and .NET. In this...
Read more >
Get the name of an array : Name « Reflection « Java Tutorial
Get the name of an array : Name « Reflection « Java Tutorial ; 7.14.1. Getting the Name of a Member Object ;...
Read more >
Name an array constant - Microsoft Support
To name an array constant, click Formula, then Define Name. Enter a name and the constant, then you can use the constant as...
Read more >
C# Array: How To Declare, Initialize And Access An Array In C#?
An array can be declared by using a data type name followed by a square bracket followed by the name of the array....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found