Skip to content

Commit 718120d

Browse files
committed
Do not widen literal types when emitting declarations
1 parent 3af710e commit 718120d

8 files changed

+73
-8
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47230,7 +47230,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4723047230
// Get type of the symbol if this is the valid symbol otherwise get type at location
4723147231
const symbol = getSymbolOfDeclaration(declaration);
4723247232
let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
47233-
? getWidenedLiteralType(getTypeOfSymbol(symbol))
47233+
? getTypeOfSymbol(symbol)
4723447234
: errorType;
4723547235
if (
4723647236
type.flags & TypeFlags.UniqueESSymbol &&

tests/baselines/reference/ambientConstLiterals.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ declare const c9: {
7474
declare const c10: number[];
7575
declare const c11: string;
7676
declare const c12: number;
77-
declare const c13: string;
78-
declare const c14: number;
77+
declare const c13: "abc" | "def";
78+
declare const c14: 123 | 456;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatterns2.ts] ////
2+
3+
//// [declarationEmitBindingPatterns2.ts]
4+
// https://github.com/microsoft/TypeScript/issues/55439
5+
6+
function foo(): { y: 1 } {
7+
return { y: 1 };
8+
}
9+
10+
export const { y = 0 } = foo();
11+
12+
13+
14+
15+
//// [declarationEmitBindingPatterns2.d.ts]
16+
export declare const y: 1 | 0;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatterns2.ts] ////
2+
3+
=== declarationEmitBindingPatterns2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55439
5+
6+
function foo(): { y: 1 } {
7+
>foo : Symbol(foo, Decl(declarationEmitBindingPatterns2.ts, 0, 0))
8+
>y : Symbol(y, Decl(declarationEmitBindingPatterns2.ts, 2, 17))
9+
10+
return { y: 1 };
11+
>y : Symbol(y, Decl(declarationEmitBindingPatterns2.ts, 3, 10))
12+
}
13+
14+
export const { y = 0 } = foo();
15+
>y : Symbol(y, Decl(declarationEmitBindingPatterns2.ts, 6, 14))
16+
>foo : Symbol(foo, Decl(declarationEmitBindingPatterns2.ts, 0, 0))
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/declarationEmitBindingPatterns2.ts] ////
2+
3+
=== declarationEmitBindingPatterns2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55439
5+
6+
function foo(): { y: 1 } {
7+
>foo : () => { y: 1;}
8+
>y : 1
9+
10+
return { y: 1 };
11+
>{ y: 1 } : { y: 1; }
12+
>y : 1
13+
>1 : 1
14+
}
15+
16+
export const { y = 0 } = foo();
17+
>y : 1 | 0
18+
>0 : 0
19+
>foo() : { y: 1; }
20+
>foo : () => { y: 1; }
21+

tests/baselines/reference/strictFunctionTypes1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ declare function fs(x: string): void;
8282
declare function fx(f: (x: "def") => void): void;
8383
declare const x1: (x: string) => void;
8484
declare const x2 = "abc";
85-
declare const x3: string;
85+
declare const x3: "def" | "abc";
8686
declare const x4: Func<string>;
8787
declare const never: never;
8888
declare const x10: string;

tests/baselines/reference/stringLiteralTypesOverloads02.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ declare namespace Consts1 {
104104
declare const string = "string";
105105
declare const number = "number";
106106
declare const boolean = "boolean";
107-
declare const stringOrNumber: string;
108-
declare const stringOrBoolean: string;
109-
declare const booleanOrNumber: string;
110-
declare const stringOrBooleanOrNumber: string;
107+
declare const stringOrNumber: "string" | "number";
108+
declare const stringOrBoolean: "string" | "boolean";
109+
declare const booleanOrNumber: "number" | "boolean";
110+
declare const stringOrBooleanOrNumber: "string" | "number" | "boolean";
111111
declare namespace Consts2 {
112112
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @strict: true
2+
// @declaration: true
3+
// @emitDeclarationOnly: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/55439
6+
7+
function foo(): { y: 1 } {
8+
return { y: 1 };
9+
}
10+
11+
export const { y = 0 } = foo();

0 commit comments

Comments
 (0)