question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strict null checks for Map members

See original GitHub issue

TypeScript Version: 2.0.0-beta Code

export interface INode {
    type: string;
    parentNode?: INode;
}

export interface IIdentifierNode extends INode {
    name: string;
}

public static isIdentifierNode (node: INode): node is IIdentifierNode {
    return node.type === NodeType.Identifier;
}

var namesMap = new Map<string, string>();

// main part
if (Nodes.isIdentifierNode(node) && namesMap.has(node.name)) {
    node.name = namesMap.get(node.name);  //`Type 'string | undefined' is not assignable to type 'string'.`
}

Expected behavior: No errors

Actual behavior: Type 'string | undefined' is not assignable to type 'string'.

Looks like here no TypeGuards for Maps?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:111
  • Comments:29 (7 by maintainers)

github_iconTop GitHub Comments

42reactions
MoussaAdamcommented, Aug 27, 2020

any progress?

27reactions
use-strictcommented, Jul 13, 2016

This effectively forces users to add the postfix ! operator for every Map#get. It doesn’t look like something that can be statically analyzed by the compiler. There are two consequences, which to be honest, make me want to give up using strict null checks.

A. Type inference for return values is no longer reliable. Given that null or undefined can’t be automatically stripped in certain cases, like above, one can get an incorrect return type. Because the behavior is unpredictable, this means one has to always write the return type annotations and not use type inference. This is very inconvenient.

Example:

function getNumber(map: Map<string, number>, key: string, defaultValue: number) {
    if (map.has(key)) {
        return map.get(key);
    }
    return defaultValue;
}

The inferred return type is number|undefined, even though it can never be undefined.

B. The explicit ! acts just as a type assertion so it suffers from the same limitations. It’s an unconditional bail from the compiler checks. Since there is no way to relate it to the condition (Map#has in this case), once the condition is changed, the user also has to revisit the !. This is just as error-prone as not having strict null checks and remembering to check against null values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Usage of Map<> with strictNullChecks
Show activity on this post. Typing of Map explicitly states that get can result in undefined : interface Map<K, V> { ... get(key:...
Read more >
Documentation - TypeScript 2.0
In strict null checking mode the compiler requires every reference to a local variable of a type that doesn't include undefined to be...
Read more >
A case study on strict null checks - Hacker News
I'm a huge fan of strictNullChecks but it is remarkable how much language features contribute towards making non-nullable types ergonomic.
Read more >
Really Advanced Typescript Types - Tableau Engineering Blog
If you enable strict null checks, you don't need to check for your ... By slapping a void member on a type, we've...
Read more >
TypeScript
strict; noImplicitAny; strictNullChecks; strictFunctionTypes ... Project Options; Strict Checks; Module Resolution; Source Maps; Linter Checks; Experimental ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found