From d08960fbd3400d0581dde5c1cd92c972bc638bc2 Mon Sep 17 00:00:00 2001 From: gcnew Date: Sun, 23 Apr 2017 19:12:56 +0300 Subject: [PATCH 1/2] Allow exporting of consts even if `strictNullChecks` is on --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cb28cb0bd5c..d43bd907c46 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11807,7 +11807,7 @@ namespace ts { // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || - type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node)) || + type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || isInAmbientContext(declaration); const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) : type === autoType || type === autoArrayType ? undefinedType : From 9c2a74c26c9b161803a20f0c8fac09a88750ba8c Mon Sep 17 00:00:00 2001 From: gcnew Date: Sun, 23 Apr 2017 19:11:01 +0300 Subject: [PATCH 2/2] Update tests --- .../reference/exportBinding.errors.txt | 27 +++++++++++++++++++ tests/baselines/reference/exportBinding.js | 24 +++++++++++++++-- .../conformance/es6/modules/exportBinding.ts | 12 +++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/exportBinding.errors.txt diff --git a/tests/baselines/reference/exportBinding.errors.txt b/tests/baselines/reference/exportBinding.errors.txt new file mode 100644 index 00000000000..552c048faf7 --- /dev/null +++ b/tests/baselines/reference/exportBinding.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/es6/modules/exportConsts.ts(3,16): error TS2448: Block-scoped variable 'x' used before its declaration. +tests/cases/conformance/es6/modules/exportConsts.ts(3,16): error TS2454: Variable 'x' is used before being assigned. +tests/cases/conformance/es6/modules/exportVars.ts(3,16): error TS2454: Variable 'y' is used before being assigned. + + +==== tests/cases/conformance/es6/modules/exportConsts.ts (2 errors) ==== + export { x } + export { x as xx } + export default x; + ~ +!!! error TS2448: Block-scoped variable 'x' used before its declaration. + ~ +!!! error TS2454: Variable 'x' is used before being assigned. + + const x = 'x' + + export { Y as Z } + class Y {} + +==== tests/cases/conformance/es6/modules/exportVars.ts (1 errors) ==== + export { y } + export { y as yy } + export default y; + ~ +!!! error TS2454: Variable 'y' is used before being assigned. + var y = 'y' + \ No newline at end of file diff --git a/tests/baselines/reference/exportBinding.js b/tests/baselines/reference/exportBinding.js index 5059c5356a3..3e0f23ad31d 100644 --- a/tests/baselines/reference/exportBinding.js +++ b/tests/baselines/reference/exportBinding.js @@ -1,19 +1,39 @@ -//// [exportBinding.ts] +//// [tests/cases/conformance/es6/modules/exportBinding.ts] //// + +//// [exportConsts.ts] export { x } +export { x as xx } +export default x; + const x = 'x' export { Y as Z } class Y {} +//// [exportVars.ts] +export { y } +export { y as yy } +export default y; +var y = 'y' -//// [exportBinding.js] + +//// [exportConsts.js] "use strict"; exports.__esModule = true; +exports["default"] = x; var x = 'x'; exports.x = x; +exports.xx = x; var Y = (function () { function Y() { } return Y; }()); exports.Z = Y; +//// [exportVars.js] +"use strict"; +exports.__esModule = true; +exports["default"] = y; +var y = 'y'; +exports.y = y; +exports.yy = y; diff --git a/tests/cases/conformance/es6/modules/exportBinding.ts b/tests/cases/conformance/es6/modules/exportBinding.ts index 1e6b0314f0b..4d9fd7e09fb 100644 --- a/tests/cases/conformance/es6/modules/exportBinding.ts +++ b/tests/cases/conformance/es6/modules/exportBinding.ts @@ -1,5 +1,17 @@ +// @filename: exportConsts.ts +// @strict: true export { x } +export { x as xx } +export default x; + const x = 'x' export { Y as Z } class Y {} + +// @filename: exportVars.ts +// @strict: true +export { y } +export { y as yy } +export default y; +var y = 'y'