Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: type-level assertion operator (!) #17948

Closed
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
13 changes: 9 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7986,11 +7986,12 @@ namespace ts {
return getTypeFromIntersectionTypeNode(<IntersectionTypeNode>node);
case SyntaxKind.JSDocNullableType:
return getTypeFromJSDocNullableTypeNode(<JSDocNullableType>node);
case SyntaxKind.ParenthesizedType:
case SyntaxKind.JSDocNonNullableType:
return getTypeFromNonNullableType((<JSDocNullableType>node).type);
case SyntaxKind.ParenthesizedType:
case SyntaxKind.JSDocOptionalType:
case SyntaxKind.JSDocTypeExpression:
return getTypeFromTypeNode((<ParenthesizedTypeNode | JSDocTypeReferencingNode | JSDocTypeExpression>node).type);
return getTypeFromTypeNode((<ParenthesizedTypeNode | JSDocTypeReferencingNode>node).type);
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructorType:
case SyntaxKind.TypeLiteral:
Expand All @@ -8016,6 +8017,10 @@ namespace ts {
}
}

function getTypeFromNonNullableType(node: TypeNode) {
return getNonNullableType(getTypeFromTypeNode(node));
}

function instantiateList<T>(items: T[], mapper: TypeMapper, instantiator: (item: T, mapper: TypeMapper) => T): T[] {
if (items && items.length) {
const result: T[] = [];
Expand Down Expand Up @@ -22374,7 +22379,8 @@ namespace ts {
return checkUnionOrIntersectionType(<UnionOrIntersectionTypeNode>node);
case SyntaxKind.ParenthesizedType:
case SyntaxKind.TypeOperator:
return checkSourceElement((<ParenthesizedTypeNode | TypeOperatorNode>node).type);
case SyntaxKind.JSDocNonNullableType:
return checkSourceElement((<ParenthesizedTypeNode | TypeOperatorNode | JSDocNonNullableType>node).type);
case SyntaxKind.JSDocComment:
return checkJSDocComment(node as JSDoc);
case SyntaxKind.JSDocParameterTag:
Expand All @@ -22383,7 +22389,6 @@ namespace ts {
checkSignatureDeclaration(node as JSDocFunctionType);
// falls through
case SyntaxKind.JSDocVariadicType:
case SyntaxKind.JSDocNonNullableType:
case SyntaxKind.JSDocNullableType:
case SyntaxKind.JSDocAllType:
case SyntaxKind.JSDocUnknownType:
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ namespace ts {
return emitEnumMember(<EnumMember>node);

// JSDoc nodes (ignored)
case SyntaxKind.JSDocNonNullableType:
return emitNonNullType(<JSDocNonNullableType>node);

// Transformation nodes (ignored)
}

Expand Down Expand Up @@ -1413,6 +1416,11 @@ namespace ts {
write("!");
}

function emitNonNullType(node: JSDocNonNullableType) {
emit(node.type);
write("!");
}

function emitMetaProperty(node: MetaProperty) {
writeToken(node.keywordToken, node.pos);
write(".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(13,14): error TS802
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(14,11): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(15,8): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(16,15): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(17,11): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(18,17): error TS8020: JSDoc types can only be used inside documentation comments.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,5): error TS2322: Type 'undefined' is not assignable to type 'number | null'.
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,17): error TS8020: JSDoc types can only be used inside documentation comments.


==== tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts (14 errors) ====
==== tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts (12 errors) ====
// grammar error from checker
var ara: Array.<number> = [1,2,3];
~
Expand Down Expand Up @@ -52,11 +50,7 @@ tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,17): error TS802
~~~~~~~~~~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
var most: !string = 'definite';
~~~~~~~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
var postfixdef: number! = 101;
~~~~~~~
!!! error TS8020: JSDoc types can only be used inside documentation comments.
var postfixopt: number? = undefined;
~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'number | null'.
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/nonNullType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [nonNullType.ts]
let a: string | undefined | null | never;
let b: typeof a!;
type Assert<T> = T!;
let c: Assert<typeof a>;


//// [nonNullType.js]
var a;
var b;
var c;
18 changes: 18 additions & 0 deletions tests/baselines/reference/nonNullType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/nonNullType.ts ===
let a: string | undefined | null | never;
>a : Symbol(a, Decl(nonNullType.ts, 0, 3))

let b: typeof a!;
>b : Symbol(b, Decl(nonNullType.ts, 1, 3))
>a : Symbol(a, Decl(nonNullType.ts, 0, 3))

type Assert<T> = T!;
>Assert : Symbol(Assert, Decl(nonNullType.ts, 1, 17))
>T : Symbol(T, Decl(nonNullType.ts, 2, 12))
>T : Symbol(T, Decl(nonNullType.ts, 2, 12))

let c: Assert<typeof a>;
>c : Symbol(c, Decl(nonNullType.ts, 3, 3))
>Assert : Symbol(Assert, Decl(nonNullType.ts, 1, 17))
>a : Symbol(a, Decl(nonNullType.ts, 0, 3))

19 changes: 19 additions & 0 deletions tests/baselines/reference/nonNullType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/nonNullType.ts ===
let a: string | undefined | null | never;
>a : string | null | undefined
>null : null

let b: typeof a!;
>b : string
>a : string | null | undefined

type Assert<T> = T!;
>Assert : T
>T : T
>T : T

let c: Assert<typeof a>;
>c : string | null | undefined
>Assert : T
>a : string | null | undefined

5 changes: 5 additions & 0 deletions tests/cases/compiler/nonNullType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @strictNullChecks: true
let a: string | undefined | null | never;
let b: typeof a!;
type Assert<T> = T!;
let c: Assert<typeof a>;
4 changes: 0 additions & 4 deletions tests/cases/fourslash/codeFixChangeJSDocSyntax7.ts

This file was deleted.