Skip to content

Commit 1500665

Browse files
committed
refactor: better TS typing for the docs app
There were some TS erorrs regaring icons, type some any-s correctly
1 parent 0ed177a commit 1500665

File tree

6 files changed

+29
-62
lines changed

6 files changed

+29
-62
lines changed

packages/__docs__/buildScripts/DataTypes.mts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,16 @@ export type ParsedDocSummary = Record<string,{
224224
tags?: string
225225
}>
226226

227-
type IconGlyph = {
227+
type Glyph = {
228+
bidirectional: boolean
229+
lineSrc: string
230+
solidSrc: string
228231
name: string
229-
variant: any
230232
glyphName: string
231-
deprecated: boolean
232-
}
233-
234-
type IconFormat = {
235-
format: 'React' | 'SVG' | 'Font'
236-
glyphs: Record<string, IconGlyph>
237-
packageName: string
238-
requirePath: string
239233
}
240234

241235
type MainIconsData = {
242-
packageName: string
243-
formats: Record<'icons-svg' | `icons-react` | 'icons-font', IconFormat>
236+
glyphs: Glyph[]
244237
}
245238

246239
type MainDocsData = {
@@ -264,8 +257,7 @@ export type {
264257
ObjectSignatureType,
265258
BaseType,
266259
LibraryOptions,
267-
IconFormat,
268-
IconGlyph,
260+
Glyph,
269261
MainDocsData,
270262
MainIconsData,
271263
JsDocResult

packages/__docs__/src/App/index.tsx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class App extends Component<AppProps, AppState> {
124124
themeKey: undefined,
125125
layout: 'large',
126126
docsData: null,
127-
versionsData: null,
127+
versionsData: undefined,
128128
iconsData: null
129129
}
130130
}
@@ -165,34 +165,6 @@ class App extends Component<AppProps, AppState> {
165165
}
166166
}
167167

168-
/**
169-
* Get every static prop from an object (inherited ones too)
170-
* @param object The object to check
171-
* @returns {Set<string>} the properties
172-
*/
173-
getAllPropNames(object: Record<string, any>) {
174-
let obj: object | null = object
175-
const props: Set<string> = new Set()
176-
// exclude some common static props for performance
177-
const invalidKeys = [
178-
'$$typeof',
179-
'render',
180-
'propTypes',
181-
'selector',
182-
'defaultProps',
183-
'displayName',
184-
'generateComponentTheme'
185-
]
186-
while (obj) {
187-
const keys = Object.keys(obj)
188-
keys.forEach((k) => {
189-
if (!invalidKeys.includes(k)) props.add(k)
190-
})
191-
obj = Reflect.getPrototypeOf(obj)
192-
}
193-
return props
194-
}
195-
196168
componentDidMount() {
197169
this._defaultDocumentTitle = document.title
198170
this.updateKey()
@@ -416,7 +388,7 @@ class App extends Component<AppProps, AppState> {
416388
<Heading level="h1" as="h2" margin="0 0 medium">
417389
Icons
418390
</Heading>
419-
<IconsPage glyphs={iconsData.glyphs} />
391+
<IconsPage glyphs={iconsData!.glyphs} />
420392
</View>
421393
)
422394

@@ -453,9 +425,9 @@ class App extends Component<AppProps, AppState> {
453425
const { themes } = this.state.docsData!
454426
const { layout, themeKey, versionsData } = this.state
455427
const { olderVersionsGitBranchMap } = versionsData || {}
456-
let legacyGitBranch
428+
let legacyGitBranch: string | undefined = undefined
457429

458-
if (olderVersionsGitBranchMap) {
430+
if (olderVersionsGitBranchMap && versionInPath) {
459431
legacyGitBranch = olderVersionsGitBranchMap[versionInPath]
460432
}
461433

packages/__docs__/src/App/props.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,14 @@ type AppState = {
8282
layout: LayoutSize
8383
showMenu: boolean
8484
key?: string
85-
versionsData: any
85+
versionsData?: {
86+
// like `v10`
87+
latestVersion: string
88+
// like `['v9', 'v10']`
89+
previousVersions: string[]
90+
// like `{"v8": "v8_maintenance", "v9": "v9_maintenance"}`
91+
olderVersionsGitBranchMap: Record<string, string>
92+
}
8693
// changelog.md read from the JSON file
8794
changelogData?: DocData
8895
// the currently shown document

packages/__docs__/src/Document/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import type {
3131
} from '@instructure/shared-types'
3232
import { DocData } from '../App/props'
3333

34-
type DocDataType = DocData & { legacyGitBranch: any }
34+
type DocDataType = DocData & { legacyGitBranch?: string }
3535

3636
type DocumentOwnProps = {
3737
doc: DocDataType

packages/__docs__/src/Icons/index.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ import * as InstIcons from '@instructure/ui-icons'
4545
import { IconXSolid } from '@instructure/ui-icons'
4646
import { Link } from '@instructure/ui-link'
4747
import { Flex } from '@instructure/ui-flex'
48-
49-
type Glyph = {
50-
lineSrc: string
51-
solidSrc: string
52-
name: string
53-
glyphName: string
54-
}
48+
import { Glyph } from '../../buildScripts/DataTypes.mjs'
5549

5650
type Format = 'react' | 'svg' | 'font'
5751

@@ -108,6 +102,7 @@ const IconTile = memo(
108102
const componentName = `${name}${
109103
styleType === 'line' ? 'Line' : 'Solid'
110104
}`
105+
// @ts-ignore TS cant type import *
111106
const IconComponent = InstIcons[componentName]
112107
return <IconComponent />
113108
} else if (format === 'svg') {
@@ -263,7 +258,7 @@ const IconsPage = ({ glyphs }: IconsPageProps) => {
263258
>
264259
{Object.keys(formats).map((f) => (
265260
<SimpleSelect.Option value={f} id={f} key={f}>
266-
{formats[f]}
261+
{formats[f as keyof typeof formats]}
267262
</SimpleSelect.Option>
268263
))}
269264
</SimpleSelect>
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@
3232
* @param signal {AbortController.signal}
3333
* @returns {Promise<null|object>}
3434
*/
35-
const fetchVersionData = async (signal) => {
35+
const fetchVersionData = async (signal: AbortSignal) => {
3636
const isLocalHost = window.location.hostname === 'localhost'
37-
3837
if (!isLocalHost) {
3938
const isGhPages = window.location.origin === 'https://instructure.github.io'
40-
const versionsDataUrl = window.location.origin + (isGhPages ? '/instructure-ui' : '') + '/versions.json'
39+
const versionsDataUrl =
40+
window.location.origin +
41+
(isGhPages ? '/instructure-ui' : '') +
42+
'/versions.json'
4143
const result = await fetch(versionsDataUrl, { signal })
4244
return await result.json()
4345
}
44-
45-
return null
46+
return undefined
4647
}
4748

4849
/**
4950
* if we are on the docs page of a legacy version,
5051
* the path includes the version number, e.g. `/v7` or `/v8`
5152
*/
5253
const versionMatch = window.location.pathname.match(/\/(v\d+)(\/|$)/)
53-
const versionInPath = versionMatch ? versionMatch[1] : null
54+
const versionInPath = versionMatch ? versionMatch[1] : null
5455

5556
export default fetchVersionData
5657
export { fetchVersionData, versionInPath }

0 commit comments

Comments
 (0)