TypeScript 2.4.1 - TS2345 - "Property 'push' is missing in type

Created on 28 Jun 2017  路  7Comments  路  Source: microsoft/TypeScript



TypeScript Version: 2.4.1

I will preface this with me being unclear whether this is a bug or my misunderstanding of the error (more likely), but I hope I can get some traction on it here nevertheless, please. And, thank you!

Code

This code used to work with 2.3.4 but fails in 2.4.1

// A *self-contained* demonstration of the problem follows...
module app {
    class TypeScript24Controller {
        static $inject = ['$log'];

        constructor(private $log: ng.ILogService) {
            $log.debug('TypeScript24Controller constructor called.');
        }
    }

    angular.module('app').controller('TypeScript24Controller', TypeScript24Controller);
}

Stripping private in my constructor eliminates the error, but I do want the private property created.

module app {
    class TypeScript24Controller {
        static $inject = ['$log'];

        constructor($log: ng.ILogService) {
            $log.debug('TypeScript24Controller constructor called.');
        }
    }

    angular.module('app').controller('TypeScript24Controller', TypeScript24Controller);
}

image

Most helpful comment

@andy-ms, Reading fully through #16047, it looks like there may not be an option to explicitly turn off the check and it's more of a "press forward and fix" approach. However, there appears to be an out with a cast. Turning

angular.module('app').controller('TypeScript24Controller', TypeScript24Controller);

into

angular.module('app').controller('TypeScript24Controller', TypeScript24Controller as any);

silences the error. I understand that that may be masking it, but it would also be somewhat inconsequential for me in this particular instance, I believe.

All 7 comments

This is due to new weak type checking (#16047).
Should have been fixed by DefinitelyTyped/DefinitelyTyped#17303. Try upgrading your @types/angular?

@andy-ms, I think that will push me to Angular 1.6 type definitions, but we only run Angular 1.5 (Angular did not use semver back then, and we've encountered some issues with 1.6). Alternatively to upgrading type definitions, is it possible to explicitly turn of weak type checking?

No, but you could use a similar fix to the one in DefinitelyTyped/DefinitelyTyped#17303; augment the interface with an index signature.

interface Options {
    a?: number;
    b?: number;
}

// Won't compile without this
interface Options {
    [x: string]: any;
}

class C {}
const options: Options = C;

@andy-ms, Reading fully through #16047, it looks like there may not be an option to explicitly turn off the check and it's more of a "press forward and fix" approach. However, there appears to be an out with a cast. Turning

angular.module('app').controller('TypeScript24Controller', TypeScript24Controller);

into

angular.module('app').controller('TypeScript24Controller', TypeScript24Controller as any);

silences the error. I understand that that may be masking it, but it would also be somewhat inconsequential for me in this particular instance, I believe.

@andy-ms, ah ok, so simply by introducing one (perhaps inconsequential), non-optional property or method in the interface to mark the type as non-weak, we get by the new weak type check in 2.4? Good to know. Would you say my approach is fair for the use-case I have? Looking for small, yet workable changes. And thanks again for all the info!

Either is fine, although as any is less safe as your types won't be checked if you try to implement any of IController's optional methods.

Totally valid point. I'll experiment with both approaches and see what ends up being preferable until we can do an angular/typings upgrade. As these are both valid workarounds, I will close this issue. Many thanks, @andy-ms!

Was this page helpful?
0 / 5 - 0 ratings