Skip to content

Commit

Permalink
Merge pull request #15330 from gcnew/exportConsts
Browse files Browse the repository at this point in the history
Allow exporting const variables when `strictNullChecks` is on
  • Loading branch information
sandersn committed Apr 24, 2017
2 parents bb8a875 + 9c2a74c commit 5783435
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -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 :
Expand Down
27 changes: 27 additions & 0 deletions 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'

24 changes: 22 additions & 2 deletions 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;
12 changes: 12 additions & 0 deletions 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'

0 comments on commit 5783435

Please sign in to comment.