-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: types for
<script setup>
macros
- Loading branch information
Showing
8 changed files
with
337 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { useAttrs, useSlots, SetupContext } from '../index' | ||
import { describe, expectType } from './utils' | ||
|
||
describe('defineProps w/ type declaration', () => { | ||
// type declaration | ||
const props = defineProps<{ | ||
foo: string | ||
}>() | ||
// explicitly declared type should be refined | ||
expectType<string>(props.foo) | ||
// @ts-expect-error | ||
props.bar | ||
}) | ||
|
||
describe('defineProps w/ type declaration + withDefaults', () => { | ||
const res = withDefaults( | ||
defineProps<{ | ||
number?: number | ||
arr?: string[] | ||
obj?: { x: number } | ||
fn?: (e: string) => void | ||
x?: string | ||
genStr?: string | ||
}>(), | ||
{ | ||
number: 123, | ||
arr: () => [], | ||
obj: () => ({ x: 123 }), | ||
fn: () => {}, | ||
genStr: () => '' | ||
} | ||
) | ||
|
||
res.number + 1 | ||
res.arr.push('hi') | ||
res.obj.x | ||
res.fn('hi') | ||
// @ts-expect-error | ||
res.x.slice() | ||
res.genStr.slice() | ||
}) | ||
|
||
describe('defineProps w/ union type declaration + withDefaults', () => { | ||
withDefaults( | ||
defineProps<{ | ||
union1?: number | number[] | { x: number } | ||
union2?: number | number[] | { x: number } | ||
union3?: number | number[] | { x: number } | ||
union4?: number | number[] | { x: number } | ||
}>(), | ||
{ | ||
union1: 123, | ||
union2: () => [123], | ||
union3: () => ({ x: 123 }), | ||
union4: () => 123 | ||
} | ||
) | ||
}) | ||
|
||
describe('defineProps w/ runtime declaration', () => { | ||
// runtime declaration | ||
const props = defineProps({ | ||
foo: String, | ||
bar: { | ||
type: Number, | ||
default: 1 | ||
}, | ||
baz: { | ||
type: Array, | ||
required: true | ||
} | ||
}) | ||
expectType<{ | ||
foo?: string | ||
bar: number | ||
baz: unknown[] | ||
}>(props) | ||
|
||
props.foo && props.foo + 'bar' | ||
props.bar + 1 | ||
// @ts-expect-error should be readonly | ||
props.bar++ | ||
props.baz.push(1) | ||
|
||
const props2 = defineProps(['foo', 'bar']) | ||
props2.foo + props2.bar | ||
// @ts-expect-error | ||
props2.baz | ||
}) | ||
|
||
describe('defineEmits w/ type declaration', () => { | ||
const emit = defineEmits<(e: 'change') => void>() | ||
emit('change') | ||
// @ts-expect-error | ||
emit() | ||
// @ts-expect-error | ||
emit('bar') | ||
|
||
type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void } | ||
const emit2 = defineEmits<Emits>() | ||
|
||
emit2('foo') | ||
emit2('bar') | ||
emit2('baz', 123) | ||
// @ts-expect-error | ||
emit2('baz') | ||
}) | ||
|
||
describe('defineEmits w/ runtime declaration', () => { | ||
const emit = defineEmits({ | ||
foo: () => {}, | ||
bar: null | ||
}) | ||
emit('foo') | ||
emit('bar', 123) | ||
// @ts-expect-error | ||
emit('baz') | ||
|
||
const emit2 = defineEmits(['foo', 'bar']) | ||
emit2('foo') | ||
emit2('bar', 123) | ||
// @ts-expect-error | ||
emit2('baz') | ||
}) | ||
|
||
describe('useAttrs', () => { | ||
const attrs = useAttrs() | ||
expectType<Record<string, unknown>>(attrs) | ||
}) | ||
|
||
describe('useSlots', () => { | ||
const slots = useSlots() | ||
expectType<SetupContext['slots']>(slots) | ||
}) |
Oops, something went wrong.