Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +713 to +714
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was largely unsure about this given you ask a question about node then use that to work with assertionNode, but this is effectively the same check as:

    function typeFromPropertyAssignment(node: PropertyAssignment | ShorthandPropertyAssignment, symbol: Symbol, context: SyntacticTypeNodeBuilderContext) {
        const typeAnnotation = getEffectiveTypeAnnotationNode(node);
        let result;
        if (typeAnnotation && resolver.canReuseTypeNodeAnnotation(context, node, typeAnnotation, symbol)) {
            result = serializeExistingTypeNode(typeAnnotation, context);
        }

So I definitely like this change more than the initial version.

}
}
return result ?? inferTypeOfDeclaration(node, symbol, context, /*reportFallback*/ false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] ////

//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts]
type Wrapper<T> = {
_type: T;
};

declare function stringWrapper(): Wrapper<string>;

declare function objWrapper<T extends Record<string, Wrapper<any>>>(
obj: T,
): Wrapper<T>;

const value = objWrapper({
prop1: stringWrapper() as Wrapper<"hello">,
});

type Unwrap<T> = T extends Wrapper<any>
? T["_type"] extends Record<string, Wrapper<any>>
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> }
: T["_type"]
: never;

declare function unwrap<T>(wrapper: T): Unwrap<T>;

export const unwrapped = unwrap(value);




//// [declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.d.ts]
export declare const unwrapped: {
prop1: "hello";
};
Comment on lines +31 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakebailey here is the requested dts test

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] ////

=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts ===
type Wrapper<T> = {
>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<string>;
>stringWrapper : Symbol(stringWrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 2, 2))
>Wrapper : Symbol(Wrapper, Decl(declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts, 0, 0))

declare function objWrapper<T extends Record<string, Wrapper<any>>>(
>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<T>;
>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> = T extends Wrapper<any>
>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<string, Wrapper<any>>
>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<T["_type"][Key]> }
>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<T>(wrapper: T): Unwrap<T>;
>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))

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/compiler/declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts] ////

=== declarationAssertionNodeNotReusedWhenTypeNotEquivalent1.ts ===
type Wrapper<T> = {
>Wrapper : Wrapper<T>
> : ^^^^^^^^^^

_type: T;
>_type : T
> : ^

};

declare function stringWrapper(): Wrapper<string>;
>stringWrapper : () => Wrapper<string>
> : ^^^^^^

declare function objWrapper<T extends Record<string, Wrapper<any>>>(
>objWrapper : <T extends Record<string, Wrapper<any>>>(obj: T) => Wrapper<T>
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^

obj: T,
>obj : T
> : ^

): Wrapper<T>;

const value = objWrapper({
>value : Wrapper<{ prop1: Wrapper<"hello">; }>
> : ^^^^^^^^^^^^^^^^^ ^^^^
>objWrapper({ prop1: stringWrapper() as Wrapper<"hello">,}) : Wrapper<{ prop1: Wrapper<"hello">; }>
> : ^^^^^^^^^^^^^^^^^ ^^^^
>objWrapper : <T extends Record<string, Wrapper<any>>>(obj: T) => Wrapper<T>
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
>{ prop1: stringWrapper() as Wrapper<"hello">,} : { prop1: Wrapper<"hello">; }
> : ^^^^^^^^^ ^^^

prop1: stringWrapper() as Wrapper<"hello">,
>prop1 : Wrapper<"hello">
> : ^^^^^^^^^^^^^^^^
>stringWrapper() as Wrapper<"hello"> : Wrapper<"hello">
> : ^^^^^^^^^^^^^^^^
>stringWrapper() : Wrapper<string>
> : ^^^^^^^^^^^^^^^
>stringWrapper : () => Wrapper<string>
> : ^^^^^^

});

type Unwrap<T> = T extends Wrapper<any>
>Unwrap : Unwrap<T>
> : ^^^^^^^^^

? T["_type"] extends Record<string, Wrapper<any>>
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> }
: T["_type"]
: never;

declare function unwrap<T>(wrapper: T): Unwrap<T>;
>unwrap : <T>(wrapper: T) => Unwrap<T>
> : ^ ^^ ^^ ^^^^^
>wrapper : T
> : ^

export const unwrapped = unwrap(value);
>unwrapped : { prop1: "hello"; }
> : ^^^^^^^^^^^^^^^^^^^
>unwrap(value) : { prop1: "hello"; }
> : ^^^^^^^^^^^^^^^^^^^
>unwrap : <T>(wrapper: T) => Unwrap<T>
> : ^ ^^ ^^ ^^^^^
>value : Wrapper<{ prop1: Wrapper<"hello">; }>
> : ^^^^^^^^^^^^^^^^^ ^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @strict: true
// @declaration: true
// @emitDeclarationOnly: true

type Wrapper<T> = {
_type: T;
};

declare function stringWrapper(): Wrapper<string>;

declare function objWrapper<T extends Record<string, Wrapper<any>>>(
obj: T,
): Wrapper<T>;

const value = objWrapper({
prop1: stringWrapper() as Wrapper<"hello">,
});

type Unwrap<T> = T extends Wrapper<any>
? T["_type"] extends Record<string, Wrapper<any>>
? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> }
: T["_type"]
: never;

declare function unwrap<T>(wrapper: T): Unwrap<T>;

export const unwrapped = unwrap(value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference path="fourslash.ts" />

// https://github.com/microsoft/TypeScript/issues/60573

// @strict: true

//// type Wrapper<T> = {
//// _type: T;
//// };
////
//// function stringWrapper(): Wrapper<string> {
//// return { _type: "" };
//// }
////
//// function objWrapper<T extends Record<string, Wrapper<any>>>(
//// obj: T,
//// ): Wrapper<T> {
//// return { _type: obj };
//// }
////
//// const value = objWrapper({
//// prop1: stringWrapper() as Wrapper<"hello">,
//// });
////
//// type Unwrap<T extends Wrapper<any>> = T["_type"] extends Record<
//// string,
//// Wrapper<any>
//// >
//// ? { [Key in keyof T["_type"]]: Unwrap<T["_type"][Key]> }
//// : T["_type"];
////
//// type Test/*1*/ = Unwrap<typeof value>;

verify.quickInfoAt("1", `type Test = {
prop1: "hello";
}`)
Loading