I am trying to limit the possible values of singleType
variable to LANGUAGES
, SCIENCE
and FICTION
instead of an open string using an enum
export enum BookType {
LANGUAGES = "LANGUAGES",
SCIENCE = "SCIENCE",
FICTION = "FICTION",
}
const singleType: BookType = 'SCIENCE'
export interface UserBooks {
name: string
type: BookType
}
const fullBook: UserBooks = {
name: 'Some string',
type: 'SCIENCE'
}
I am getting this errors:
const singleType: BookType
'singleType' is declared but its value is never read.ts(6133)
Type '"SCIENCE"' is not assignable to type 'BookType'.ts(2322)
(property) UserBooks.type: BookType
Type '"SCIENCE"' is not assignable to type 'BookType'.ts(2322)
section.ts(92, 3): The expected type comes from property 'type' which is declared here on type 'UserBooks'