Skip to content

Negated Types: Consider assertions invertible and create negated narrowings in false branches of assertion narrowings - #65296

Closed
Wesley Wigham (weswigham) wants to merge 24 commits into
mainfrom
weswigham/negated-types-invertible-assertions
Closed

Wesley Wigham (weswigham) wants to merge 24 commits into
mainfrom
weswigham/negated-types-invertible-assertions

Conversation

@weswigham

Copy link
Copy Markdown
Member

Just like with conditionals, this isn't strictly sound to do. In particular, this trips over assertions which are less specific than the check they do.

For example, isSimpleTypeReference(p: any): p is TypeReference makes total sense today, but if you narrow to typeof p & not TypeReference in the false branch, you're just kinda wrong - it could still be a type reference (just not a "simple" one) - the specificity of the types and the implementation the guard checks don't match, and nothing actually forces them to - you could argue that it's even common for a type guard to produce a type less specific than what the guard actually checks, simply because that's the type that exists, and it didn't matter that it was overbroad before.

Deduplicate fresh negations against their regular counterparts during union collection and intersection insertion. Preserve the first intersection position and ordered-set membership when replacing a fresh member, while leaving standalone fresh negations eligible for widening.

Add compiler coverage for explicit exclusions, generic array narrowing, both union operand orders, and fresh-only widening.
…ntial structures, broaden negation reduction applicability
@typescript-automation typescript-automation Bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 3, 2026
@weswigham

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) test this

@typescript-automation

Copy link
Copy Markdown
Contributor

Hey Wesley Wigham (@weswigham), this PR is in an unmergable state, so is missing a merge commit to run against; please resolve conflicts and try again.

@weswigham
Wesley Wigham (weswigham) force-pushed the weswigham/negated-types-invertible-assertions branch from 640c71d to 8db7e43 Compare November 23, 2026 20:23
@weswigham

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) test this

@typescript-automation

typescript-automation Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started
user test this ✅ Started 👀 Results
run dt ✅ Started 👀 Results
perf test this faster ✅ Started ❌ Results

@typescript-automation

Copy link
Copy Markdown
Contributor

Hey Wesley Wigham (@weswigham), the results of running the DT tests are ready.

There were interesting changes:

Branch only errors: jquery/v3

Package: jquery/v3

Branch error:

Project scope: tsconfig.json

Error: types/jquery/v3/jquery-tests.ts:683:17
TypeScript@local expected type to be:
  object
got:
  object & not any[]

types/jquery/v3/jquery-tests.ts:700:17
TypeScript@local expected type to be:
  object
got:
  object & not Function

types/jquery/v3/jquery-tests.ts:740:17
TypeScript@local expected type to be:
  object
got:
  object & not Window
    at combineErrorsAndWarnings (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:200:28)
    at runTests (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:192:20)
Branch only errors: regex-not

Package: regex-not

Branch error:

Project scope: tsconfig.json

Error: types/regex-not/regex-not-tests.ts:4:19
TypeScript@local compile error TS1110:
Type expected.
    at combineErrorsAndWarnings (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:200:28)
    at runTests (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:192:20)
Branch only errors: nwsapi

Package: nwsapi

Branch error:

Project scope: tsconfig.json

Error: types/nwsapi/test/nwsapi.cjs.test.ts:30:5
TypeScript@local expected type to be:
  Element
got:
  Element | SVGSVGElement

types/nwsapi/test/nwsapi.umd.test.ts:30:5
TypeScript@local expected type to be:
  Element
got:
  Element | SVGSVGElement
    at combineErrorsAndWarnings (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:200:28)
    at runTests (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:192:20)
Branch only errors: lodash/v3

Package: lodash/v3

Branch error:

Project scope: tsconfig.json

Error: types/lodash/v3/lodash-tests.ts:7025:13
TypeScript@local expected type to be:
  string
got:
  string & not ""

types/lodash/v3/lodash-tests.ts:7039:13
TypeScript@local expected type to be:
  { value?: boolean; }
got:
  { value?: boolean; } & not EmptyObject<{ value?: boolean; }>
    at combineErrorsAndWarnings (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:200:28)
    at runTests (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:192:20)
Branch only errors: lodash

Package: lodash

Branch error:

Project scope: tsconfig.json

Error: types/lodash/lodash-tests.ts:4138:13
TypeScript@local expected type to be:
  { a: string; }
got:
  { a: string; } & not { length: number; }

types/lodash/lodash-tests.ts:4144:13
TypeScript@local expected type to be:
  { a: string; }
got:
  { a: string; } & not { length: number; }

types/lodash/lodash-tests.ts:4228:13
TypeScript@local expected type to be:
  { a: string; }
got:
  { a: string; } & not (object & { length: number; })

types/lodash/lodash-tests.ts:4234:13
TypeScript@local expected type to be:
  { a: string; }
got:
  { a: string; } & not (object & { length: number; })

types/lodash/lodash-tests.ts:4339:9
TypeScript@local expected type to be:
  string
got:
  string & not ""

types/lodash/lodash-tests.ts:4370:9
TypeScript@local expected type to be:
  { value?: boolean | undefined; }
got:
  { value?: boolean | undefined; } & not EmptyObject<{ value?: boolean | undefined; }>

types/lodash/lodash-tests.ts:5691:9
TypeScript@local expected type to be:
  AbcObject
got:
  AbcObject & not (AbcObject & { d: unknown; } & { [uniqueSymbol]: unknown; })

types/lodash/lodash-tests.ts:5697:9
TypeScript@local expected type to be:
  Partial<AbcObject>
got:
  Partial<AbcObject> & not (Partial<AbcObject> & { a: number | undefined; } & { [uniqueSymbol]: unknown; })

types/lodash/lodash-tests.ts:5706:9
TypeScript@local expected type to be:
  Record<string, number | string>
got:
  Record<string, string | number> & not (Record<string, string | number> & { a: string | number; } & { [uniqueSymbol]: unknown; })
    at combineErrorsAndWarnings (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:200:28)
    at runTests (/mnt/vss/_work/1/s/DefinitelyTyped-tools/packages/dtslint/dist/index.js:192:20)

Full output in the log.

@typescript-automation

Copy link
Copy Markdown
Contributor

Wesley Wigham (@weswigham) Here are the results of running the user tests with tsc comparing baseline and pr:

Something interesting changed - please have a look.

Details

effect

packages/tools/ai-docgen/tsconfig.json

packages/platform/cloudflare/tsconfig.alchemy.json

lodash

/mnt/vss/work/1/s/ts_downloads//m/lodash/tsconfig.json

  • [NEW] error TS2345: Argument of type 'symbol | (string & not "__proto__" & not "constructor" & not "prototype")' is not assignable to parameter of type 'string'.
    • /mnt/vss/work/1/s/ts_downloads//m/lodash/node_modules/lodash/_baseSet.js(45,25)
  • [MISSING] error TS2345: Argument of type 'string | symbol' is not assignable to parameter of type 'string'.
    • /mnt/vss/work/1/s/ts_downloads//m/lodash/node_modules/lodash/_baseSet.js(45,25)

type-fest

tsconfig.json

webpack

tsconfig.json

tsconfig.types.json

tsconfig.types.test.json

@typescript-automation

Copy link
Copy Markdown
Contributor

Wesley Wigham (@weswigham), the perf run you requested failed. You can check the log here.

@weswigham

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) pack this

@typescript-automation

typescript-automation Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@github-actions

Copy link
Copy Markdown

Hey, Wesley Wigham (@weswigham)! I've packed 8db7e43 into an installable package. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "npm:@typescript-deploys/pr-build@7.1.0-pr.65296.8"
    }
}

and then running npm install.

There is also a playground for this build.

@weswigham

Copy link
Copy Markdown
Member Author

Experiment done, work will continue in #64375 - this one might be followed up on in the future after negated types merge - it really wants new syntax to distinguish between invertible/partitioning type guards and not, which is not something we want to bundle into another new syntax proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant