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

Infer AssertsIdentifier type predicates #58495

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
accept error baseline
danvk committed May 15, 2024

Verified

This commit was signed with the committer’s verified signature.
danvk Dan Vanderkam
commit b242dbdbd293f1eb5f2f0f9b57b75e124d80ea80
21 changes: 20 additions & 1 deletion tests/baselines/reference/inferTypePredicates.errors.txt
Original file line number Diff line number Diff line change
@@ -15,9 +15,10 @@ inferTypePredicates.ts(115,7): error TS2322: Type 'string | number' is not assig
Type 'string' is not assignable to type 'number'.
inferTypePredicates.ts(133,7): error TS2740: Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1' but required in type 'C2'.
inferTypePredicates.ts(446,3): error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.


==== inferTypePredicates.ts (11 errors) ====
==== inferTypePredicates.ts (12 errors) ====
// https://github.com/microsoft/TypeScript/issues/16069

const numsOrNull = [1, 2, 3, 4, null];
@@ -479,4 +480,22 @@ inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1
throw new Error();
}
}

// should return void, not "asserts pattern is string"
const assertWithFuncExpr = function (pattern: unknown) {
if (typeof pattern !== 'string') {
throw new TypeError('invalid pattern')
}

if (pattern.length > 1024) {
throw new TypeError('pattern is too long')
}
}

function useAssertWithFuncExpr(pattern: string) {
assertWithFuncExpr(pattern);
~~~~~~~~~~~~~~~~~~
!!! error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
!!! related TS2782 inferTypePredicates.ts:435:7: 'assertWithFuncExpr' needs an explicit type annotation.
}

29 changes: 29 additions & 0 deletions tests/baselines/reference/inferTypePredicates.js
Original file line number Diff line number Diff line change
@@ -433,6 +433,21 @@ function inferFromTypePredAny(x: any) {
throw new Error();
}
}

// should return void, not "asserts pattern is string"
const assertWithFuncExpr = function (pattern: unknown) {
if (typeof pattern !== 'string') {
throw new TypeError('invalid pattern')
}

if (pattern.length > 1024) {
throw new TypeError('pattern is too long')
}
}

function useAssertWithFuncExpr(pattern: string) {
assertWithFuncExpr(pattern);
}


//// [inferTypePredicates.js]
@@ -826,6 +841,18 @@ function inferFromTypePredAny(x) {
throw new Error();
}
}
// should return void, not "asserts pattern is string"
var assertWithFuncExpr = function (pattern) {
if (typeof pattern !== 'string') {
throw new TypeError('invalid pattern');
}
if (pattern.length > 1024) {
throw new TypeError('pattern is too long');
}
};
function useAssertWithFuncExpr(pattern) {
assertWithFuncExpr(pattern);
}


//// [inferTypePredicates.d.ts]
@@ -946,3 +973,5 @@ declare function assertName(x: any): asserts x is Named;
declare function isNamed(x: any): x is Named;
declare function inferFromTypePred(x: unknown): asserts x is Named;
declare function inferFromTypePredAny(x: any): asserts x is Named;
declare const assertWithFuncExpr: (pattern: unknown) => asserts pattern is string;
declare function useAssertWithFuncExpr(pattern: string): void;
31 changes: 31 additions & 0 deletions tests/baselines/reference/inferTypePredicates.symbols
Original file line number Diff line number Diff line change
@@ -1105,3 +1105,34 @@ function inferFromTypePredAny(x: any) {
}
}

// should return void, not "asserts pattern is string"
const assertWithFuncExpr = function (pattern: unknown) {
>assertWithFuncExpr : Symbol(assertWithFuncExpr, Decl(inferTypePredicates.ts, 434, 5))
>pattern : Symbol(pattern, Decl(inferTypePredicates.ts, 434, 37))

if (typeof pattern !== 'string') {
>pattern : Symbol(pattern, Decl(inferTypePredicates.ts, 434, 37))

throw new TypeError('invalid pattern')
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}

if (pattern.length > 1024) {
>pattern.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>pattern : Symbol(pattern, Decl(inferTypePredicates.ts, 434, 37))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

throw new TypeError('pattern is too long')
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}
}

function useAssertWithFuncExpr(pattern: string) {
>useAssertWithFuncExpr : Symbol(useAssertWithFuncExpr, Decl(inferTypePredicates.ts, 442, 1))
>pattern : Symbol(pattern, Decl(inferTypePredicates.ts, 444, 31))

assertWithFuncExpr(pattern);
>assertWithFuncExpr : Symbol(assertWithFuncExpr, Decl(inferTypePredicates.ts, 434, 5))
>pattern : Symbol(pattern, Decl(inferTypePredicates.ts, 444, 31))
}

65 changes: 65 additions & 0 deletions tests/baselines/reference/inferTypePredicates.types
Original file line number Diff line number Diff line change
@@ -2248,3 +2248,68 @@ function inferFromTypePredAny(x: any) {
}
}

// should return void, not "asserts pattern is string"
const assertWithFuncExpr = function (pattern: unknown) {
>assertWithFuncExpr : (pattern: unknown) => asserts pattern is string
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>function (pattern: unknown) { if (typeof pattern !== 'string') { throw new TypeError('invalid pattern') } if (pattern.length > 1024) { throw new TypeError('pattern is too long') }} : (pattern: unknown) => asserts pattern is string
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>pattern : unknown
> : ^^^^^^^

if (typeof pattern !== 'string') {
>typeof pattern !== 'string' : boolean
> : ^^^^^^^
>typeof pattern : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>pattern : unknown
> : ^^^^^^^
>'string' : "string"
> : ^^^^^^^^

throw new TypeError('invalid pattern')
>new TypeError('invalid pattern') : TypeError
> : ^^^^^^^^^
>TypeError : TypeErrorConstructor
> : ^^^^^^^^^^^^^^^^^^^^
>'invalid pattern' : "invalid pattern"
> : ^^^^^^^^^^^^^^^^^
}

if (pattern.length > 1024) {
>pattern.length > 1024 : boolean
> : ^^^^^^^
>pattern.length : number
> : ^^^^^^
>pattern : string
> : ^^^^^^
>length : number
> : ^^^^^^
>1024 : 1024
> : ^^^^

throw new TypeError('pattern is too long')
>new TypeError('pattern is too long') : TypeError
> : ^^^^^^^^^
>TypeError : TypeErrorConstructor
> : ^^^^^^^^^^^^^^^^^^^^
>'pattern is too long' : "pattern is too long"
> : ^^^^^^^^^^^^^^^^^^^^^
}
}

function useAssertWithFuncExpr(pattern: string) {
>useAssertWithFuncExpr : (pattern: string) => void
> : ^ ^^ ^^^^^^^^^
>pattern : string
> : ^^^^^^

assertWithFuncExpr(pattern);
>assertWithFuncExpr(pattern) : void
> : ^^^^
>assertWithFuncExpr : (pattern: unknown) => asserts pattern is string
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>pattern : string
> : ^^^^^^
}

15 changes: 15 additions & 0 deletions tests/cases/compiler/inferTypePredicates.ts
Original file line number Diff line number Diff line change
@@ -433,3 +433,18 @@ function inferFromTypePredAny(x: any) {
throw new Error();
}
}

// should return void, not "asserts pattern is string"
const assertWithFuncExpr = function (pattern: unknown) {
if (typeof pattern !== 'string') {
throw new TypeError('invalid pattern')
}

if (pattern.length > 1024) {
throw new TypeError('pattern is too long')
}
}

function useAssertWithFuncExpr(pattern: string) {
assertWithFuncExpr(pattern);
}