|
1 | 1 | import { BindingTypes } from '@vue/compiler-core' |
2 | 2 | import { assertCode, compileSFCScript as compile } from '../utils' |
| 3 | +import { invalidateTypeCache } from '../../src/script/resolveType' |
3 | 4 |
|
4 | 5 | describe('defineProps', () => { |
5 | 6 | test('basic usage', () => { |
@@ -829,4 +830,97 @@ const props = defineProps({ foo: String }) |
829 | 830 | expect(content).toMatch(`foo: { default: 5.5, type: Number }`) |
830 | 831 | assertCode(content) |
831 | 832 | }) |
| 833 | + |
| 834 | + test.each([false, true])( |
| 835 | + 'keeps imported composite defaults from adding Boolean casting (isProd: %s)', |
| 836 | + isProd => { |
| 837 | + const files: Record<string, string> = { |
| 838 | + '/composite-props.ts': ` |
| 839 | + type Local = string |
| 840 | + export type Props<T, U = T | Local> = { flag?: U } |
| 841 | + `, |
| 842 | + } |
| 843 | + invalidateTypeCache('/composite-props.ts') |
| 844 | + const { content } = compile( |
| 845 | + `<script setup lang="ts"> |
| 846 | + import type { Props } from './composite-props' |
| 847 | + type Local = boolean |
| 848 | + defineProps<Props<number>>() |
| 849 | + </script>`, |
| 850 | + { |
| 851 | + isProd, |
| 852 | + fs: { |
| 853 | + fileExists: file => file in files, |
| 854 | + readFile: file => files[file], |
| 855 | + }, |
| 856 | + }, |
| 857 | + { filename: '/Consumer.vue' }, |
| 858 | + ) |
| 859 | + expect(content).toContain( |
| 860 | + isProd |
| 861 | + ? 'flag: {}' |
| 862 | + : 'flag: { type: [Number, String], required: false }', |
| 863 | + ) |
| 864 | + }, |
| 865 | + ) |
| 866 | + |
| 867 | + test.each([false, true])( |
| 868 | + 'infers mapped values for each property key (isProd: %s)', |
| 869 | + isProd => { |
| 870 | + const { content } = compile( |
| 871 | + `<script setup lang="ts"> |
| 872 | + type Values = { enabled: boolean; label: string } |
| 873 | + type Props = { [K in 'enabled' | 'label']?: Values[K] } |
| 874 | + defineProps<Props>() |
| 875 | + </script>`, |
| 876 | + { isProd }, |
| 877 | + ) |
| 878 | + expect(content).toContain( |
| 879 | + isProd |
| 880 | + ? 'enabled: { type: Boolean }' |
| 881 | + : 'enabled: { type: Boolean, required: false }', |
| 882 | + ) |
| 883 | + expect(content).toContain( |
| 884 | + isProd ? 'label: {}' : 'label: { type: String, required: false }', |
| 885 | + ) |
| 886 | + }, |
| 887 | + ) |
| 888 | + |
| 889 | + test.each([false, true])( |
| 890 | + 'preserves caller scope when extracting array elements (isProd: %s)', |
| 891 | + isProd => { |
| 892 | + const files: Record<string, string> = { |
| 893 | + '/array-helpers.ts': ` |
| 894 | + type Local = boolean |
| 895 | + export type Element<T extends readonly unknown[]> = T[number] |
| 896 | + `, |
| 897 | + } |
| 898 | + invalidateTypeCache('/array-helpers.ts') |
| 899 | + const { content } = compile( |
| 900 | + `<script setup lang="ts"> |
| 901 | + import type { Element } from './array-helpers' |
| 902 | + type Local = string |
| 903 | + defineProps<{ |
| 904 | + array?: Element<Local[]> |
| 905 | + tuple?: Element<[Local]> |
| 906 | + namedTuple?: Element<[value: Local]> |
| 907 | + reference?: Element<Array<Local>> |
| 908 | + }>() |
| 909 | + </script>`, |
| 910 | + { |
| 911 | + isProd, |
| 912 | + fs: { |
| 913 | + fileExists: file => file in files, |
| 914 | + readFile: file => files[file], |
| 915 | + }, |
| 916 | + }, |
| 917 | + { filename: '/ArrayConsumer.vue' }, |
| 918 | + ) |
| 919 | + for (const key of ['array', 'tuple', 'namedTuple', 'reference']) { |
| 920 | + expect(content).toContain( |
| 921 | + isProd ? `${key}: {}` : `${key}: { type: String, required: false }`, |
| 922 | + ) |
| 923 | + } |
| 924 | + }, |
| 925 | + ) |
832 | 926 | }) |
0 commit comments