问题

在vite.config.ts中配置全局替换变量:

  1. define: {
  2. __DEV__: true
  3. }

使用:
image.png
因为没有声明DEV,所以ts会报错

解决方法

看了一下Vue核心库中是使用了global.d.ts进行全局声明

  1. /// <reference types="vite/client" />
  2. // Global compile-time constants
  3. declare var __DEV__: boolean

然后就解决啦。

vuejs-core中的global.d.ts

  1. /// <reference types="vite/client" />
  2. // Global compile-time constants
  3. declare var __DEV__: boolean
  4. declare var __TEST__: boolean
  5. declare var __BROWSER__: boolean
  6. declare var __GLOBAL__: boolean
  7. declare var __ESM_BUNDLER__: boolean
  8. declare var __ESM_BROWSER__: boolean
  9. declare var __NODE_JS__: boolean
  10. declare var __SSR__: boolean
  11. declare var __COMMIT__: string
  12. declare var __VERSION__: string
  13. declare var __COMPAT__: boolean
  14. // Feature flags
  15. declare var __FEATURE_OPTIONS_API__: boolean
  16. declare var __FEATURE_PROD_DEVTOOLS__: boolean
  17. declare var __FEATURE_SUSPENSE__: boolean
  18. // for tests
  19. declare namespace jest {
  20. interface Matchers<R, T> {
  21. toHaveBeenWarned(): R
  22. toHaveBeenWarnedLast(): R
  23. toHaveBeenWarnedTimes(n: number): R
  24. }
  25. }
  26. declare module '*.vue' {}
  27. declare module 'file-saver' {
  28. export function saveAs(blob: any, name: any): void
  29. }
  30. declare module '@vue/repl' {
  31. import { ComponentOptions } from '@vue/runtime-core'
  32. const Repl: ComponentOptions
  33. const ReplStore: any
  34. export { Repl, ReplStore }
  35. }
  36. declare interface String {
  37. /**
  38. * @deprecated Please use String.prototype.slice instead of String.prototype.substring in the repository.
  39. */
  40. substring(start: number, end?: number): string
  41. }