Skip to content

Commit

Permalink
fix(language-core): align types of v-for with core (vuejs#5084)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX authored Jan 22, 2025
1 parent b89dbee commit 07a1baa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
11 changes: 4 additions & 7 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,20 @@ export function generateGlobalTypes(options: VueCompilerOptions) {
>>;
type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;
function __VLS_getVForSourceType(source: number): [number, number, number][];
function __VLS_getVForSourceType(source: string): [string, number, number][];
function __VLS_getVForSourceType(source: number): [number, number][];
function __VLS_getVForSourceType(source: string): [string, number][];
function __VLS_getVForSourceType<T extends any[]>(source: T): [
item: T[number],
key: number,
index: number,
][];
function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [
item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never,
key: number,
index: undefined,
index: number,
][];
// #3845
function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [
item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never),
key: number,
index: undefined,
index: number,
][];
function __VLS_getVForSourceType<T>(source: T): [
item: T[keyof T],
Expand Down
5 changes: 0 additions & 5 deletions test-workspace/tsc/passedFixtures/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ type IfEquals<T, U, Y = unknown, N = never> =
(<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? Y : N;
export declare function exactType<T, U>(draft: T & IfEquals<T, U>, expected: U & IfEquals<T, U>): IfEquals<T, U>;

// https://stackoverflow.com/a/49928360
type IfNotAny<T> = 0 extends 1 & T ? never : T;
type IfNotUndefined<T> = Exclude<T, undefined> extends never ? never : T;
export declare function isNotAnyOrUndefined<T>(value: IfNotAny<IfNotUndefined<T>>): void;
46 changes: 17 additions & 29 deletions test-workspace/tsc/passedFixtures/vue3/v-for/main.vue
Original file line number Diff line number Diff line change
@@ -1,65 +1,53 @@
<template>
<!-- number -->
<div v-for="(val, key) in 10">
<div v-for="(val, index) in 10">
{{ exactType(val, {} as number) }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as number) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- string -->
<div v-for="(val, key) in 'foo'">
<div v-for="(val, index) in 'foo'">
{{ exactType(val, {} as string) }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as number) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- array -->
<div v-for="(val, key) in arr">
<div v-for="(val, index) in arr">
{{ exactType(val, {} as 'a' | 'b') }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as number) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- map -->
<div v-for="(val, key) in map">
<div v-for="(val, index) in map">
{{ exactType(val, {} as [string, number]) }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as number) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- obj -->
<div v-for="(val, key) in obj">
<div v-for="(val, key, index) in obj">
{{ exactType(val, {} as string | number) }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as 'a' | 'b') }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- objUnion -->
<div v-for="(val, key) in objUnion">
<div v-for="(val, key, index) in objUnion">
<!-- {{ exactType(val, {} as string | number) }} -->
{{ exactType(val, {} as string) }}
{{ isNotAnyOrUndefined(val) }}
<!-- {{ exactType(key, {} as 'a' | 'b') }} -->
{{ exactType(key, {} as 'a') }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- record -->
<div v-for="(val, key) in record">
<div v-for="(val, key, index) in record">
{{ exactType(val, {} as string) }}
{{ isNotAnyOrUndefined(val) }}
{{ exactType(key, {} as string) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
<!-- any -->
<div v-for="(val, key) in _any">
<div v-for="(val, index) in _any">
{{ exactType(val, {} as any) }}
{{ exactType(key, {} as number) }}
{{ isNotAnyOrUndefined(key) }}
{{ exactType(index, {} as number) }}
</div>
</template>

<script setup lang="ts">
import { exactType, isNotAnyOrUndefined } from '../../shared';
import { exactType } from '../../shared';
const arr = ['a', 'b'] as const;
const map = new Map<string, number>();
Expand Down

0 comments on commit 07a1baa

Please sign in to comment.