I am trying to implement an Enum - composed of string - in an attempt to make my code more robust.
The method addSkills()
received (among others) a skills parameters which is a string array.
Here is the test code :
it('should record skills into database', async () => {
const id = '19'
const token = 'a64a47cc4c3c6282c229df78aab373c3d0dd94c4'
const newSkill = 'Friendly'
await service.addSkills(id, token, [newSkill])
const recordedSkills = service.getCandidateSkills(id)
expect(recordedSkills).toContain('Friendly')
})
})
Here is the temporary production code :
async addSkills(
id: string,
token: string,
skills: SoftSkillsEnum[]
): Promise<[] | null> {
throw new HttpException('Unknown resource', HttpStatus.NOT_FOUND)
}
export enum SoftSkillsEnum {
CREATIVE = 'Creative',
FUNNY = 'Funny',
EMPATHETIC = 'Empathetic',
EXPLORER = 'Explorer',
SPORTY = 'Sporty',
SUPER_SMART = 'Super Smart',
FRIENDLY = 'Friendly',
}
The test code trigger the Typescript error on line 5 of the test code:
TS2322: Type '"Friendly"' is not assignable to type 'SoftSkillsEnum'.
As far as I understand it, Typescript make a difference between a string and an enum composed of string. But I don't want my method to accept any string, the input should match a predefined list of string (the predefined "softsSkills").
Anyway to make it work or I must give up my enum implementation attempt?