From 354fd10a2e12e49407615648579c8c00ee34e94a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 1 Apr 2016 09:53:44 -0700 Subject: [PATCH] Separate error messages for 'null', 'undefined', or both. --- src/compiler/checker.ts | 10 ++++++++-- src/compiler/diagnosticMessages.json | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index da6548f8c69..6fc60c5c78a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9554,8 +9554,14 @@ namespace ts { function checkNonNullExpression(node: Expression | QualifiedName) { const type = checkExpression(node); - if (strictNullChecks && getNullableKind(type)) { - error(node, Diagnostics.Object_is_possibly_null_or_undefined); + if (strictNullChecks) { + const kind = getNullableKind(type); + if (kind) { + error(node, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ? + Diagnostics.Object_is_possibly_null_or_undefined : + Diagnostics.Object_is_possibly_undefined : + Diagnostics.Object_is_possibly_null); + } return getNonNullableType(type); } return type; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index d7c2fac2710..ebd3202d3a7 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1727,10 +1727,18 @@ "category": "Error", "code": 2530 }, - "Object is possibly 'null' or 'undefined'.": { + "Object is possibly 'null'.": { "category": "Error", "code": 2531 }, + "Object is possibly 'undefined'.": { + "category": "Error", + "code": 2532 + }, + "Object is possibly 'null' or 'undefined'.": { + "category": "Error", + "code": 2533 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600