Strict null checks for Map members
See original GitHub issueTypeScript 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:
- Created 7 years ago
- Reactions:111
- Comments:29 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
any progress?
This effectively forces users to add the postfix
!
operator for everyMap#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:
The inferred return type is
number|undefined
, even though it can never beundefined
.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.