My question is similar to this one, but with a twist. I want to create a string literal union type, but the values from the initial array come from a package (json data file), and are obtained through "require"
The most common scenario is like this :
const namesArr = ["John", "Lily", "Roy"] as const;
type Names = typeof namesArr[number]; // "John" | "Lily" | "Roy"
But my scenario is like this :
const namesArr = require('names-package');
type Names = typeof namesArr[number]; // "John" | "Lily" | "Roy"
My problem is that Typescript thinks that type "Names" is really "any".
const name:Names = "John"; //Visual studio code shows "name" as being of type "any".
I can see inside the package that the data comes from a basic Json file : index.json
[
"John",
"Lily",
"Roy"
]
Is there anything I can do about it?