Skip to content

Commit 97dedeb

Browse files
authored
feat(types): update to Typescript 3.9 (#1106)
1 parent 6cd97f0 commit 97dedeb

17 files changed

+140
-470
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"lint": "prettier --write --parser typescript \"packages/**/*.ts?(x)\"",
1212
"ls-lint": "ls-lint",
1313
"test": "node scripts/build.js vue -f global -d && jest",
14-
"test-dts": "node scripts/build.js shared reactivity runtime-core runtime-dom -dt -f esm-bundler && tsd",
14+
"test-dts": "node scripts/build.js shared reactivity runtime-core runtime-dom -dt -f esm-bundler && tsc -p ./test-dts/tsconfig.json && tsc -p ./test-dts/tsconfig.build.json",
1515
"release": "node scripts/release.js",
1616
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
1717
"dev-compiler": "npm-run-all --parallel \"dev template-explorer\" serve",
@@ -70,8 +70,7 @@
7070
"semver": "^6.3.0",
7171
"serve": "^11.3.0",
7272
"ts-jest": "^25.2.1",
73-
"tsd": "^0.11.0",
74-
"typescript": "^3.8.3",
73+
"typescript": "^3.9.3",
7574
"yorkie": "^2.0.0"
7675
}
7776
}

packages/runtime-core/src/componentOptions.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface ComponentOptionsBase<
135135

136136
// type-only differentiator to separate OptionWithoutProps from a constructor
137137
// type returned by defineComponent() or FunctionalComponent
138-
call?: never
138+
call?: (this: unknown, ...args: unknown[]) => never
139139
// type-only differentiators for built-in Vnode types
140140
__isFragment?: never
141141
__isTeleport?: never
@@ -197,9 +197,9 @@ export interface MethodOptions {
197197
}
198198

199199
export type ExtractComputedReturns<T extends any> = {
200-
[key in keyof T]: T[key] extends { get: Function }
201-
? ReturnType<T[key]['get']>
202-
: ReturnType<T[key]>
200+
[key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
201+
? TReturn
202+
: T[key] extends (...args: any[]) => infer TReturn ? TReturn : never
203203
}
204204

205205
type WatchOptionItem =

packages/runtime-core/src/components/Suspense.ts

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ function createSuspenseBoundary(
244244
/* istanbul ignore if */
245245
if (__DEV__ && !__TEST__ && !hasWarned) {
246246
hasWarned = true
247+
// @ts-ignore `console.info` cannot be null error
247248
console[console.info ? 'info' : 'log'](
248249
`<Suspense> is an experimental feature and its API will likely change.`
249250
)

packages/runtime-dom/types/jsx.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ declare global {
13311331
}
13321332
interface IntrinsicElements extends NativeElements {
13331333
// allow arbitrary elements
1334+
// @ts-ignore supress ts:2374 = Duplicate string index signature.
13341335
[name: string]: any
13351336
}
13361337
}

packages/vue/src/devCheck.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
if (__BROWSER__ && __DEV__) {
2+
// @ts-ignore `console.info` cannot be null error
23
console[console.info ? 'info' : 'log'](
34
`You are running a development build of Vue.\n` +
45
`Make sure to use the production build (*.prod.js) when deploying for production.`

test-dts/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Test-ts
2+
3+
Tests Typescript types to ensure the types remain as expected.
4+
5+
## Configuration
6+
7+
### tsconfig.json
8+
9+
Config used to test against the package source
10+
11+
### tsconfig.build.json
12+
13+
Replaces the `vue` and `@vue/*` dependencies with the built Typescript to ensure the published types are correct.

test-dts/componentTypeExtensions.test-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { expectError, expectType } from 'tsd'
2-
import { defineComponent } from './index'
1+
import { defineComponent, expectError, expectType } from './index'
32

43
declare module '@vue/runtime-core' {
54
interface ComponentCustomOptions {
@@ -20,9 +19,11 @@ export const Custom = defineComponent({
2019

2120
methods: {
2221
aMethod() {
22+
// @ts-expect-error
2323
expectError(this.notExisting)
2424
this.counter++
2525
this.state = 'running'
26+
// @ts-expect-error
2627
expectError((this.state = 'not valid'))
2728
}
2829
}

test-dts/defineComponent.test-d.tsx

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { expectError, expectType } from 'tsd'
21
import {
32
describe,
43
defineComponent,
54
PropType,
65
ref,
76
reactive,
8-
createApp
7+
createApp,
8+
expectError,
9+
expectType
910
} from './index'
1011

1112
describe('with object props', () => {
@@ -83,7 +84,7 @@ describe('with object props', () => {
8384
expectType<ExpectedProps['eee']>(props.eee)
8485
expectType<ExpectedProps['fff']>(props.fff)
8586

86-
// props should be readonly
87+
// @ts-expect-error props should be readonly
8788
expectError((props.a = 1))
8889

8990
// setup context
@@ -112,7 +113,7 @@ describe('with object props', () => {
112113
expectType<ExpectedProps['eee']>(props.eee)
113114
expectType<ExpectedProps['fff']>(props.fff)
114115

115-
// props should be readonly
116+
// @ts-expect-error props should be readonly
116117
expectError((props.a = 1))
117118

118119
// should also expose declared props on `this`
@@ -129,7 +130,7 @@ describe('with object props', () => {
129130
expectType<ExpectedProps['eee']>(this.eee)
130131
expectType<ExpectedProps['fff']>(this.fff)
131132

132-
// props on `this` should be readonly
133+
// @ts-expect-error props on `this` should be readonly
133134
expectError((this.a = 1))
134135

135136
// assert setup context unwrapping
@@ -167,13 +168,14 @@ describe('with object props', () => {
167168
/>
168169
)
169170

170-
// missing required props
171+
// @ts-expect-error missing required props
171172
expectError(<MyComponent />)
172173

173-
// wrong prop types
174174
expectError(
175+
// @ts-expect-error wrong prop types
175176
<MyComponent a={'wrong type'} b="foo" dd={{ n: 1 }} ddd={['foo']} />
176177
)
178+
// @ts-expect-error
177179
expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
178180
})
179181

@@ -211,7 +213,7 @@ describe('type inference w/ array props declaration', () => {
211213
defineComponent({
212214
props: ['a', 'b'],
213215
setup(props) {
214-
// props should be readonly
216+
// @ts-expect-error props should be readonly
215217
expectError((props.a = 1))
216218
expectType<any>(props.a)
217219
expectType<any>(props.b)
@@ -222,6 +224,7 @@ describe('type inference w/ array props declaration', () => {
222224
render() {
223225
expectType<any>(this.$props.a)
224226
expectType<any>(this.$props.b)
227+
// @ts-expect-error
225228
expectError((this.$props.a = 1))
226229
expectType<any>(this.a)
227230
expectType<any>(this.b)
@@ -340,19 +343,29 @@ describe('emits', () => {
340343
setup(props, { emit }) {
341344
emit('click', 1)
342345
emit('input', 'foo')
346+
// @ts-expect-error
343347
expectError(emit('nope'))
348+
// @ts-expect-error
344349
expectError(emit('click'))
350+
// @ts-expect-error
345351
expectError(emit('click', 'foo'))
352+
// @ts-expect-error
346353
expectError(emit('input'))
354+
// @ts-expect-error
347355
expectError(emit('input', 1))
348356
},
349357
created() {
350358
this.$emit('click', 1)
351359
this.$emit('input', 'foo')
360+
// @ts-expect-error
352361
expectError(this.$emit('nope'))
362+
// @ts-expect-error
353363
expectError(this.$emit('click'))
364+
// @ts-expect-error
354365
expectError(this.$emit('click', 'foo'))
366+
// @ts-expect-error
355367
expectError(this.$emit('input'))
368+
// @ts-expect-error
356369
expectError(this.$emit('input', 1))
357370
}
358371
})
@@ -364,12 +377,14 @@ describe('emits', () => {
364377
emit('foo')
365378
emit('foo', 123)
366379
emit('bar')
380+
// @ts-expect-error
367381
expectError(emit('nope'))
368382
},
369383
created() {
370384
this.$emit('foo')
371385
this.$emit('foo', 123)
372386
this.$emit('bar')
387+
// @ts-expect-error
373388
expectError(this.$emit('nope'))
374389
}
375390
})
+14-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { expectError, expectType } from 'tsd'
2-
import { FunctionalComponent } from './index'
1+
import { FunctionalComponent, expectError, expectType } from './index'
32

43
// simple function signature
54
const Foo = (props: { foo: number }) => props.foo
65

76
// TSX
87
expectType<JSX.Element>(<Foo foo={1} />)
9-
// expectError(<Foo />) // tsd does not catch missing type errors
8+
// @ts-expect-error
9+
expectError(<Foo />)
10+
// @ts-expect-error
1011
expectError(<Foo foo="bar" />)
12+
// @ts-expect-error
1113
expectError(<Foo baz="bar" />)
1214

1315
// Explicit signature with props + emits
@@ -18,24 +20,32 @@ const Bar: FunctionalComponent<
1820
expectType<number>(props.foo)
1921

2022
emit('update', 123)
23+
// @ts-expect-error
2124
expectError(emit('nope'))
25+
// @ts-expect-error
2226
expectError(emit('update'))
27+
// @ts-expect-error
2328
expectError(emit('update', 'nope'))
2429
}
2530

2631
// assigning runtime options
2732
Bar.props = {
2833
foo: Number
2934
}
35+
// @ts-expect-error
3036
expectError((Bar.props = { foo: String }))
3137

3238
Bar.emits = {
3339
update: value => value > 1
3440
}
41+
// @ts-expect-error
3542
expectError((Bar.emits = { baz: () => void 0 }))
3643

3744
// TSX
3845
expectType<JSX.Element>(<Bar foo={1} />)
39-
// expectError(<Foo />) // tsd does not catch missing type errors
46+
// @ts-expect-error
47+
expectError(<Foo />)
48+
// @ts-expect-error
4049
expectError(<Bar foo="bar" />)
50+
// @ts-expect-error
4151
expectError(<Foo baz="bar" />)

test-dts/h.test-d.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { expectError, expectAssignable } from 'tsd'
21
import {
32
describe,
43
h,
@@ -7,36 +6,48 @@ import {
76
Fragment,
87
Teleport,
98
Suspense,
10-
Component
9+
Component,
10+
expectError,
11+
expectAssignable
1112
} from './index'
1213

1314
describe('h inference w/ element', () => {
1415
// key
1516
h('div', { key: 1 })
1617
h('div', { key: 'foo' })
18+
// @ts-expect-error
1719
expectError(h('div', { key: [] }))
20+
// @ts-expect-error
1821
expectError(h('div', { key: {} }))
1922
// ref
2023
h('div', { ref: 'foo' })
2124
h('div', { ref: ref(null) })
2225
h('div', { ref: el => {} })
26+
// @ts-expect-error
2327
expectError(h('div', { ref: [] }))
28+
// @ts-expect-error
2429
expectError(h('div', { ref: {} }))
30+
// @ts-expect-error
2531
expectError(h('div', { ref: 123 }))
2632
})
2733

2834
describe('h inference w/ Fragment', () => {
2935
// only accepts array children
3036
h(Fragment, ['hello'])
3137
h(Fragment, { key: 123 }, ['hello'])
38+
// @ts-expect-error
3239
expectError(h(Fragment, 'foo'))
40+
// @ts-expect-error
3341
expectError(h(Fragment, { key: 123 }, 'bar'))
3442
})
3543

3644
describe('h inference w/ Teleport', () => {
3745
h(Teleport, { to: '#foo' }, 'hello')
46+
// @ts-expect-error
3847
expectError(h(Teleport))
48+
// @ts-expect-error
3949
expectError(h(Teleport, {}))
50+
// @ts-expect-error
4051
expectError(h(Teleport, { to: '#foo' }))
4152
})
4253

@@ -47,15 +58,19 @@ describe('h inference w/ Suspense', () => {
4758
h(Suspense, null, {
4859
default: () => 'foo'
4960
})
61+
// @ts-expect-error
5062
expectError(h(Suspense, { onResolve: 1 }))
5163
})
5264

5365
describe('h inference w/ functional component', () => {
5466
const Func = (_props: { foo: string; bar?: number }) => ''
5567
h(Func, { foo: 'hello' })
5668
h(Func, { foo: 'hello', bar: 123 })
69+
// @ts-expect-error
5770
expectError(h(Func, { foo: 123 }))
71+
// @ts-expect-error
5872
expectError(h(Func, {}))
73+
// @ts-expect-error
5974
expectError(h(Func, { bar: 123 }))
6075
})
6176

@@ -85,10 +100,11 @@ describe('h inference w/ defineComponent', () => {
85100
h(Foo, { bar: 1, foo: 'ok' })
86101
// should allow extraneous props (attrs fallthrough)
87102
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
88-
// should fail on missing required prop
103+
// @ts-expect-error should fail on missing required prop
89104
expectError(h(Foo, {}))
105+
// @ts-expect-error
90106
expectError(h(Foo, { foo: 'ok' }))
91-
// should fail on wrong type
107+
// @ts-expect-error should fail on wrong type
92108
expectError(h(Foo, { bar: 1, foo: 1 }))
93109
})
94110

@@ -101,10 +117,11 @@ describe('h inference w/ defineComponent + optional props', () => {
101117
h(Foo, { bar: 1, foo: 'ok' })
102118
// should allow extraneous props (attrs fallthrough)
103119
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
104-
// should fail on missing required prop
120+
// @ts-expect-error should fail on missing required prop
105121
expectError(h(Foo, {}))
122+
// @ts-expect-error
106123
expectError(h(Foo, { foo: 'ok' }))
107-
// should fail on wrong type
124+
// @ts-expect-error should fail on wrong type
108125
expectError(h(Foo, { bar: 1, foo: 1 }))
109126
})
110127

@@ -115,10 +132,11 @@ describe('h inference w/ defineComponent + direct function', () => {
115132
h(Foo, { bar: 1, foo: 'ok' })
116133
// should allow extraneous props (attrs fallthrough)
117134
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
118-
// should fail on missing required prop
135+
// @ts-expect-error should fail on missing required prop
119136
expectError(h(Foo, {}))
137+
// @ts-expect-error
120138
expectError(h(Foo, { foo: 'ok' }))
121-
// should fail on wrong type
139+
// @ts-expect-error should fail on wrong type
122140
expectError(h(Foo, { bar: 1, foo: 1 }))
123141
})
124142

test-dts/index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
export * from '@vue/runtime-dom'
88

99
export function describe(_name: string, _fn: () => void): void
10+
11+
export function expectType<T>(value: T): void
12+
export function expectError<T>(value: T): void
13+
export function expectAssignable<T, T2 extends T = T>(value: T2): void

0 commit comments

Comments
 (0)