Skip to content

Commit 4fc2174

Browse files
月迷津渡yyx990803
authored andcommitted
workflow(template-explorer): improve types (vuejs#128)
1 parent 8d70093 commit 4fc2174

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

packages/template-explorer/src/index.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import { compilerOptions, initOptions } from './options'
44
import { watch } from '@vue/runtime-dom'
55
import { SourceMapConsumer } from 'source-map'
66

7-
const self = window as any
7+
declare global {
8+
interface Window {
9+
monaco: typeof m
10+
_deps: any
11+
init: () => void
12+
}
13+
}
814

9-
self.init = () => {
10-
const monaco = (window as any).monaco as typeof m
15+
window.init = () => {
16+
const monaco = window.monaco
1117
const persistedState = JSON.parse(
1218
decodeURIComponent(window.location.hash.slice(1)) || `{}`
1319
)
@@ -28,10 +34,8 @@ self.init = () => {
2834
monaco.editor.setModelMarkers(editor.getModel()!, `@vue/compiler-dom`, [])
2935
console.log(`AST: `, ast)
3036
lastSuccessfulCode = code + `\n\n// Check the console for the AST`
31-
lastSuccessfulMap = new self._deps['source-map'].SourceMapConsumer(
32-
map
33-
) as SourceMapConsumer
34-
lastSuccessfulMap.computeColumnSpans()
37+
lastSuccessfulMap = new window._deps['source-map'].SourceMapConsumer(map)
38+
lastSuccessfulMap!.computeColumnSpans()
3539
} catch (e) {
3640
console.error(e)
3741
}
@@ -71,7 +75,7 @@ self.init = () => {
7175
}
7276
}
7377

74-
const sharedEditorOptions = {
78+
const sharedEditorOptions: m.editor.IEditorConstructionOptions = {
7579
theme: 'vs-dark',
7680
fontSize: 14,
7781
wordWrap: 'on',
@@ -81,30 +85,24 @@ self.init = () => {
8185
minimap: {
8286
enabled: false
8387
}
84-
} as const
85-
86-
const editor = monaco.editor.create(
87-
document.getElementById('source') as HTMLElement,
88-
{
89-
value: persistedState.src || `<div>Hello World!</div>`,
90-
language: 'html',
91-
...sharedEditorOptions
92-
}
93-
)
88+
}
89+
90+
const editor = monaco.editor.create(document.getElementById('source')!, {
91+
value: persistedState.src || `<div>Hello World!</div>`,
92+
language: 'html',
93+
...sharedEditorOptions
94+
})
9495

9596
editor.getModel()!.updateOptions({
9697
tabSize: 2
9798
})
9899

99-
const output = monaco.editor.create(
100-
document.getElementById('output') as HTMLElement,
101-
{
102-
value: '',
103-
language: 'javascript',
104-
readOnly: true,
105-
...sharedEditorOptions
106-
}
107-
)
100+
const output = monaco.editor.create(document.getElementById('output')!, {
101+
value: '',
102+
language: 'javascript',
103+
readOnly: true,
104+
...sharedEditorOptions
105+
})
108106
output.getModel()!.updateOptions({
109107
tabSize: 2
110108
})
@@ -207,7 +205,10 @@ self.init = () => {
207205
watch(reCompile)
208206
}
209207

210-
function debounce<T extends Function>(fn: T, delay: number = 300): T {
208+
function debounce<T extends (...args: any[]) => any>(
209+
fn: T,
210+
delay: number = 300
211+
): T {
211212
let prevTimer: NodeJS.Timeout | null = null
212213
return ((...args: any[]) => {
213214
if (prevTimer) {

packages/template-explorer/src/options.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ const App = {
5353
checked:
5454
compilerOptions.prefixIdentifiers ||
5555
compilerOptions.mode === 'module',
56-
onChange(e: any) {
56+
onChange(e: Event) {
5757
compilerOptions.prefixIdentifiers =
58-
e.target.checked || compilerOptions.mode === 'module'
58+
(<HTMLInputElement>e.target).checked ||
59+
compilerOptions.mode === 'module'
5960
}
6061
}),
6162
h('label', { for: 'prefix' }, 'prefixIdentifiers'),
@@ -65,8 +66,8 @@ const App = {
6566
type: 'checkbox',
6667
id: 'hoist',
6768
checked: compilerOptions.hoistStatic,
68-
onChange(e: any) {
69-
compilerOptions.hoistStatic = e.target.checked
69+
onChange(e: Event) {
70+
compilerOptions.hoistStatic = (<HTMLInputElement>e.target).checked
7071
}
7172
}),
7273
h('label', { for: 'hoist' }, 'hoistStatic')
@@ -76,5 +77,5 @@ const App = {
7677
}
7778

7879
export function initOptions() {
79-
createApp().mount(App, document.getElementById('header') as HTMLElement)
80+
createApp().mount(App, document.getElementById('header')!)
8081
}

0 commit comments

Comments
 (0)