diff --git a/source/lib/assertions/handlers/informational.ts b/source/lib/assertions/handlers/informational.ts index 493d6efc..95846cb6 100644 --- a/source/lib/assertions/handlers/informational.ts +++ b/source/lib/assertions/handlers/informational.ts @@ -1,7 +1,17 @@ -import {CallExpression, TypeChecker} from '@tsd/typescript'; +import {CallExpression, TypeChecker, TypeFormatFlags} from '@tsd/typescript'; import {Diagnostic} from '../../interfaces'; import {makeDiagnostic} from '../../utils'; +/** + * Default formatting flags set by TS plus the {@link TypeFormatFlags.NoTruncation NoTruncation} flag. + * + * @see {@link https://github.dev/microsoft/TypeScript/blob/b975dfa1027d1f3073fa7cbe6f7045bf4c882785/src/compiler/checker.ts#L4717 TypeChecker.typeToString} + */ +const typeToStringFormatFlags = + TypeFormatFlags.AllowUniqueESSymbolType | + TypeFormatFlags.UseAliasDefinedOutsideCurrentScope | + TypeFormatFlags.NoTruncation; + /** * Prints the type of the argument of the assertion as a warning. * @@ -19,8 +29,9 @@ export const prinTypeWarning = (checker: TypeChecker, nodes: Set for (const node of nodes) { const argumentType = checker.getTypeAtLocation(node.arguments[0]); const argumentExpression = node.arguments[0].getText(); + const typeString = checker.typeToString(argumentType, node, typeToStringFormatFlags); - diagnostics.push(makeDiagnostic(node, `Type for expression \`${argumentExpression}\` is: \`${checker.typeToString(argumentType)}\``, 'warning')); + diagnostics.push(makeDiagnostic(node, `Type for expression \`${argumentExpression}\` is: \`${typeString}\``, 'warning')); } return diagnostics; diff --git a/source/test/fixtures/print-type/index.d.ts b/source/test/fixtures/print-type/index.d.ts index 6f3a1c51..12ee5dfd 100644 --- a/source/test/fixtures/print-type/index.d.ts +++ b/source/test/fixtures/print-type/index.d.ts @@ -1 +1,17 @@ -export default function (foo: number): number | null; +export function aboveZero(foo: number): number | null; + +type SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace = { + life: 42 +}; + +export const bigType: { + prop1: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop2: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop3: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop4: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop5: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop6: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop7: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop8: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; + prop9: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; +} diff --git a/source/test/fixtures/print-type/index.test-d.ts b/source/test/fixtures/print-type/index.test-d.ts index 9a17b216..55ac5119 100644 --- a/source/test/fixtures/print-type/index.test-d.ts +++ b/source/test/fixtures/print-type/index.test-d.ts @@ -1,5 +1,5 @@ import {printType} from '../../..'; -import aboveZero from '.'; +import {aboveZero, bigType} from '.'; printType(aboveZero); printType(null); @@ -8,3 +8,4 @@ printType(null as any); printType(null as never); printType(null as unknown); printType('foo'); +printType(bigType); diff --git a/source/test/test.ts b/source/test/test.ts index 2600df8a..3529dd0f 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -453,6 +453,7 @@ test('prints the types of expressions passed to `printType` helper', async t => [8, 0, 'warning', 'Type for expression `null as never` is: `never`'], [9, 0, 'warning', 'Type for expression `null as unknown` is: `unknown`'], [10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'], + [11, 0, 'warning', 'Type for expression `bigType` is: `{ prop1: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop2: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop3: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop4: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop5: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop6: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop7: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop8: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; prop9: SuperTypeWithAnExessiveLongNameThatTakesUpTooMuchSpace; }`'] ]); });