最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript: Usage of Map<> with strictNullChecks - Stack Overflow

programmeradmin2浏览0评论

Given the following simple class:

class Observer {
        private subscribers: Map<string, Array<((data: any) => void)>> = new Map();     

        public subscribe(event: string, callback: (data: any) => void) {
            if (!this.subscribers.has(event)) {
                this.subscribers.set(event, []);
            }

            this.subscribers.get(event).push(callback); //tsc says: Object is possibly 'undefined'
        }
    }

Furthermore, in tsconfig.json, the flags strictNullChecks and strict are enabled.

Although subscribers is checked for a key of the current event, the typescript compiler complains with the error message shown above (this.subscribers.get(event) is possibly undefined).

If I'm not completely wrong, this.subscribers.get(event) can never be undefined in this case.

How can I get rid of that message?

Given the following simple class:

class Observer {
        private subscribers: Map<string, Array<((data: any) => void)>> = new Map();     

        public subscribe(event: string, callback: (data: any) => void) {
            if (!this.subscribers.has(event)) {
                this.subscribers.set(event, []);
            }

            this.subscribers.get(event).push(callback); //tsc says: Object is possibly 'undefined'
        }
    }

Furthermore, in tsconfig.json, the flags strictNullChecks and strict are enabled.

Although subscribers is checked for a key of the current event, the typescript compiler complains with the error message shown above (this.subscribers.get(event) is possibly undefined).

If I'm not completely wrong, this.subscribers.get(event) can never be undefined in this case.

How can I get rid of that message?

Share Improve this question edited Apr 17, 2019 at 18:08 belwood 3,77911 gold badges41 silver badges47 bronze badges asked Nov 19, 2017 at 11:12 tklepzigtklepzig 6381 gold badge11 silver badges21 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 22

Typing of Map explicitly states that get can result in undefined:

interface Map<K, V> {
    ...
    get(key: K): V | undefined;
    ...
}

That's why you're getting error with strictNullChecks enabled.

You can use non-null assertion operator to inform the compiler that you're sure that it actually has value:

this.subscribers.get(event)!.push(callback);

Another option (the better one in my opinion) is to refactor your code in following way:

public subscribe(event: string, callback: (data: any) => void) {
    let callbacks = this.subscribers.get(event);
    if (!callbacks) {
        callbacks = []
        this.subscribers.set(event, callbacks);
    }

    callbacks.push(callback);
}

Why not writing a new interface/class that would not tolerate undefined values?

interface StrictMap<K, V> {
    ...
    get(key: K): V /* `| undefined` -> main diff. with `Map` native interface: The values can't be undefined */;
    ...
}

PS : In fact, I was searching for such an interface/class when I found this SO post ;)

发布评论

评论列表(0)

  1. 暂无评论