R
R
Riim2015-05-15 13:43:05
typescript
Riim, 2015-05-15 13:43:05

Typescript, block cast type to variable, how?

Hello, I'm picking typescript and one question doesn't even google: let's say this code:

on(types: string|string[]|Object, fn: Function, context?: Object): Emitter {
        // types can be a view of types/handlers
        if (typeof types == 'object') {
            for (let type in <Object>types) {
                // we don't process space-separated events here for performance;
                // it's a hot path since Layer uses the on(obj) syntax
                this._on(type, types[type], fn);
            }

        }
        // ...
    }

here I cast `types`, otherwise the compiler swears. If you have to use `types` several times in this block, then you will have to cast `Object` each time, which is not convenient, or you can write something like this:
let t = <Object>types;
continue to use `t` instead of `types`. This is already convenient, but at the output a code is generated with an obviously superfluous assignment, somehow it is not beautiful, but I want the resulting code to remain normal.
Is it possible somehow to make it convenient and without govnokoda output?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Kupstas, 2015-05-26
@Illorian

So you can’t do anything here, because types can be of 3 types and each time you have to cast.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question