Can you prevent an interface property whose type is function being implemented as a method in a Class?

Viewed 68

Background

Given the following interface

interface MyInterface<I, O> {
    execute: (input: I) => O
}

The TS compiler is then happy with either of the following implementations of MyInterface:

class MyClass implements MyInterface<string, number> {

    execute = (input: string): number => 1; 
}

Or

class MyClass implements MyInterface<string, number> {

    execute(input: string): number {
        return 1;
    } 
}

Playground

Question

Due to the way type variance is handled in TS - contra-variant function arguments, but bi-variant method arguments - is it possible to prevent (by raising a compiler error) execute being implemented as a method?

0 Answers
Related