B
B
BonBon Slick2020-06-01 13:09:39
typescript
BonBon Slick, 2020-06-01 13:09:39

Argument of type '"test"' is not assignable to parameter of type 'never'?

export interface IAuth {
    roles: string[];
}

@Module({namespaced: true})
export default class Auth extends VuexModule implements IAuth {
    roles = [];

    public get addRole(role: string): void {
        this.roles.push('test')


it is solved if we add the data type again: string[]
export default class Auth extends VuexModule implements IAuth {
    roles: string[] = [];


but the meaning of the interface then disappears

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twoone, 2020-06-01
@BonBonSlick

Initially, type inference determines whether a field belongs roles = [];to a type, never[]which does not lead to an error associated with the implementation of the interface, since the never type is a subtype of all types in typescript. In simple words, in your case, the usual upcasting occurs. If this behavior were different then subtypes would not be compatible with their supertypes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question