diff --git a/src/compiler/expressionToTypeNode.ts b/src/compiler/expressionToTypeNode.ts index 30a154f636960..83f0a0f0d9a6b 100644 --- a/src/compiler/expressionToTypeNode.ts +++ b/src/compiler/expressionToTypeNode.ts @@ -706,12 +706,12 @@ export function createSyntacticTypeNodeBuilder( } if (!result && node.kind === SyntaxKind.PropertyAssignment) { const initializer = node.initializer; - const type = isJSDocTypeAssertion(initializer) ? getJSDocTypeAssertionType(initializer) : + const assertionNode = isJSDocTypeAssertion(initializer) ? getJSDocTypeAssertionType(initializer) : initializer.kind === SyntaxKind.AsExpression || initializer.kind === SyntaxKind.TypeAssertionExpression ? (initializer as AsExpression | TypeAssertion).type : undefined; - if (type && !isConstTypeReference(type)) { - result = serializeExistingTypeNode(type, context); + if (assertionNode && !isConstTypeReference(assertionNode) && resolver.canReuseTypeNodeAnnotation(context, node, assertionNode, symbol)) { + result = serializeExistingTypeNode(assertionNode, context); } } return result ?? inferTypeOfDeclaration(node, symbol, context, /*reportFallback*/ false); diff --git a/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.js b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.js new file mode 100644 index 0000000000000..dc81a99aa63d7 --- /dev/null +++ b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.js @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// + +//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] +type Wrapper = { + _type: T; +}; + +declare function stringWrapper(): Wrapper; + +declare function objWrapper>>( + obj: T, +): Wrapper; + +const value = objWrapper({ + prop1: stringWrapper() as Wrapper<"hello">, +}); + +type Unwrap = T extends Wrapper + ? T["_type"] extends Record> + ? { [Key in keyof T["_type"]]: Unwrap } + : T["_type"] + : never; + +declare function unwrap(wrapper: T): Unwrap; + +export const unwrapped = unwrap(value); + + + + +//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.d.ts] +export declare const unwrapped: { + prop1: "hello"; +}; diff --git a/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.symbols b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.symbols new file mode 100644 index 0000000000000..fcfc2f0f54962 --- /dev/null +++ b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.symbols @@ -0,0 +1,78 @@ +//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// + +=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts === +type Wrapper = { +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 13)) + + _type: T; +>_type : Symbol(_type, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 19)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 13)) + +}; + +declare function stringWrapper(): Wrapper; +>stringWrapper : Symbol(stringWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 2, 2)) +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) + +declare function objWrapper>>( +>objWrapper : Symbol(objWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 4, 50)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) + + obj: T, +>obj : Symbol(obj, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 68)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) + +): Wrapper; +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 6, 28)) + +const value = objWrapper({ +>value : Symbol(value, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 5)) +>objWrapper : Symbol(objWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 4, 50)) + + prop1: stringWrapper() as Wrapper<"hello">, +>prop1 : Symbol(prop1, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 26)) +>stringWrapper : Symbol(stringWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 2, 2)) +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) + +}); + +type Unwrap = T extends Wrapper +>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) + + ? T["_type"] extends Record> +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0)) + + ? { [Key in keyof T["_type"]]: Unwrap } +>Key : Symbol(Key, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 16, 9)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) +>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) +>Key : Symbol(Key, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 16, 9)) + + : T["_type"] +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 14, 12)) + + : never; + +declare function unwrap(wrapper: T): Unwrap; +>unwrap : Symbol(unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 18, 10)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) +>wrapper : Symbol(wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 27)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) +>Unwrap : Symbol(Unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 12, 3)) +>T : Symbol(T, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 20, 24)) + +export const unwrapped = unwrap(value); +>unwrapped : Symbol(unwrapped, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 22, 12)) +>unwrap : Symbol(unwrap, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 18, 10)) +>value : Symbol(value, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 10, 5)) + diff --git a/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.types b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.types new file mode 100644 index 0000000000000..d432618693d4f --- /dev/null +++ b/tests/baselines/reference/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.types @@ -0,0 +1,74 @@ +//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] //// + +=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts === +type Wrapper = { +>Wrapper : Wrapper +> : ^^^^^^^^^^ + + _type: T; +>_type : T +> : ^ + +}; + +declare function stringWrapper(): Wrapper; +>stringWrapper : () => Wrapper +> : ^^^^^^ + +declare function objWrapper>>( +>objWrapper : >>(obj: T) => Wrapper +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ + + obj: T, +>obj : T +> : ^ + +): Wrapper; + +const value = objWrapper({ +>value : Wrapper<{ prop1: Wrapper<"hello">; }> +> : ^^^^^^^^^^^^^^^^^ ^^^^ +>objWrapper({ prop1: stringWrapper() as Wrapper<"hello">,}) : Wrapper<{ prop1: Wrapper<"hello">; }> +> : ^^^^^^^^^^^^^^^^^ ^^^^ +>objWrapper : >>(obj: T) => Wrapper +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ +>{ prop1: stringWrapper() as Wrapper<"hello">,} : { prop1: Wrapper<"hello">; } +> : ^^^^^^^^^ ^^^ + + prop1: stringWrapper() as Wrapper<"hello">, +>prop1 : Wrapper<"hello"> +> : ^^^^^^^^^^^^^^^^ +>stringWrapper() as Wrapper<"hello"> : Wrapper<"hello"> +> : ^^^^^^^^^^^^^^^^ +>stringWrapper() : Wrapper +> : ^^^^^^^^^^^^^^^ +>stringWrapper : () => Wrapper +> : ^^^^^^ + +}); + +type Unwrap = T extends Wrapper +>Unwrap : Unwrap +> : ^^^^^^^^^ + + ? T["_type"] extends Record> + ? { [Key in keyof T["_type"]]: Unwrap } + : T["_type"] + : never; + +declare function unwrap(wrapper: T): Unwrap; +>unwrap : (wrapper: T) => Unwrap +> : ^ ^^ ^^ ^^^^^ +>wrapper : T +> : ^ + +export const unwrapped = unwrap(value); +>unwrapped : { prop1: "hello"; } +> : ^^^^^^^^^^^^^^^^^^^ +>unwrap(value) : { prop1: "hello"; } +> : ^^^^^^^^^^^^^^^^^^^ +>unwrap : (wrapper: T) => Unwrap +> : ^ ^^ ^^ ^^^^^ +>value : Wrapper<{ prop1: Wrapper<"hello">; }> +> : ^^^^^^^^^^^^^^^^^ ^^^^ + diff --git a/tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts b/tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts new file mode 100644 index 0000000000000..05cedc8b10aba --- /dev/null +++ b/tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts @@ -0,0 +1,27 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +type Wrapper = { + _type: T; +}; + +declare function stringWrapper(): Wrapper; + +declare function objWrapper>>( + obj: T, +): Wrapper; + +const value = objWrapper({ + prop1: stringWrapper() as Wrapper<"hello">, +}); + +type Unwrap = T extends Wrapper + ? T["_type"] extends Record> + ? { [Key in keyof T["_type"]]: Unwrap } + : T["_type"] + : never; + +declare function unwrap(wrapper: T): Unwrap; + +export const unwrapped = unwrap(value); diff --git a/tests/cases/fourslash/quickInfoAssertionNodeNotReusedWhenTypeNotEquivalent1.ts b/tests/cases/fourslash/quickInfoAssertionNodeNotReusedWhenTypeNotEquivalent1.ts new file mode 100644 index 0000000000000..aa14e0fa5eb1b --- /dev/null +++ b/tests/cases/fourslash/quickInfoAssertionNodeNotReusedWhenTypeNotEquivalent1.ts @@ -0,0 +1,36 @@ +/// + +// https://github.com/microsoft/TypeScript/issues/60573 + +// @strict: true + +//// type Wrapper = { +//// _type: T; +//// }; +//// +//// function stringWrapper(): Wrapper { +//// return { _type: "" }; +//// } +//// +//// function objWrapper>>( +//// obj: T, +//// ): Wrapper { +//// return { _type: obj }; +//// } +//// +//// const value = objWrapper({ +//// prop1: stringWrapper() as Wrapper<"hello">, +//// }); +//// +//// type Unwrap> = T["_type"] extends Record< +//// string, +//// Wrapper +//// > +//// ? { [Key in keyof T["_type"]]: Unwrap } +//// : T["_type"]; +//// +//// type Test/*1*/ = Unwrap; + +verify.quickInfoAt("1", `type Test = { + prop1: "hello"; +}`)