Typescript: The recommended way to ignore “property does not exist on value of type”

Created on 11 Jan 2016  ·  1Comment  ·  Source: microsoft/TypeScript

I need to override an experiment property in Chrome and encounter this error.

if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual'; // type error
}

I found the answers at: http://stackoverflow.com/questions/18083389/ignore-typescript-errors-property-does-not-exist-on-value-of-type. But not sure which one is the right way to go.

(history as any).scrollRestoration = 'manual';

vs

history['scrollRestoration'] = 'manual';

It seems that TypeScript's docs / deep dive book doesn't mention about the squared brackets syntax.

Question

Most helpful comment

I'd go with the first one. The second one works, but only if you compile without -noImplicitAny.

>All comments

I'd go with the first one. The second one works, but only if you compile without -noImplicitAny.

Was this page helpful?
0 / 5 - 0 ratings