Hi I got unused imports warnings in VSC. My file:
import * as en from '../../src/assets/i18n/en.json'
import * as fi from '../../src/assets/i18n/fi.json'
import * as sv from '../../src/assets/i18n/sv.json'
async checkFields(language) {
await expect(this.heading).toContainText(
language.EinvoicesProductRegister_Heading_Title,
)
on en.json file I got this:
"EinvoicesProductRegister_Heading_Title": "Products and services",
and I also use that checkFields method in other file:
test('Check Product register page fields', async ({ page }) => {
const productRegister = new ProductRegister(page)
productRegister.checkFields('en')
})
What should I do if I want these unused imports are not be treated as a unused in VSC when these imports are used as a parameter?
2
Answers
As already mentioned in comments the imports are not used in your code, which is the crux of your issue. But the rest of the comments are less helpful.
The issue is very basic, which indicates you would be well served by studying some programming examples. In any case, step-by-step:
you pass a value
en
into the function to indicate you want to use this particular language.you need to relate or "map" that value to one of the imports you have at the top of the file
you can use a sequence of
if()
statements or you can use aswitch()
statement, or you can use a mapping object. The mapping object is my preference so I will show it here.You should research and try out the other methods I mention as your skills need improving – the resolution of this question should be very obvious to anyone wishing to use Playwright.
Just a note that Typescript will not be happy with
when used in
const language = languageMap[languageValue];
It will complain about using the string
key
languageValue
as an index into thelanguageMap
object. One way to fix that is to use this:which is similar to
For more on this type of Typescript problem, see expression of type ‘string’ can’t be used as an index.