refactor(form): export interfaces used in createForm() #16894
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is an anti-pattern in
src/composables/form.ts
, where a.d.ts
file is generated. The internal ref field is typed with an interface but not exported. However, the external field is exported using typeof and includes the interface. This is problematic and would result in a TypeScript error, even if not using vue.js.For example, if you create a new TypeScript project with
"declaration": true
in tsconfig, and have two files: one file exports a function that uses an interface not exported in that file, and another file imports that function. TypeScript will report an error during the compilation process.TypeScript will report this error only when the following four conditions all met:
Why wasn't this anti-pattern code reported as an error by TypeScript before? This is related to the previous implementation of the Vue.js generic type
UnwrapRef
, which adopts the implementation method of constantly deconstructing types. Whenref<T>()
is called, it happens to generate a brand-new type alias for types likeFormField
andFieldValidationResult
, and type alias will not trigger this issue, so it can just work properly.But in fact, it's wrong, If the implementation of
UnwrapRef
is changed, this file will encounter typescript errors.There are two ways to rectify it: one is to export the corresponding interface and the other is to change the interface to a type declaration. Considering that other files use interfaces extensively, I adopted the first rectification approach.