-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(frontend): move utils to TypeScript #9820
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ import { | |
TAB_TYPE as TAB, | ||
} from 'src/dashboard/util/componentTypes'; | ||
|
||
const getIndentation = depth => | ||
const getIndentation = (depth: number) => | ||
Array(depth * 3) | ||
.fill('') | ||
.join('-'); | ||
|
@@ -136,20 +136,22 @@ describe('isValidChild', () => { | |
invalidExamples.forEach((example, exampleIdx) => { | ||
let childDepth = 0; | ||
example.forEach((childType, i) => { | ||
const shouldTestChild = Array.isArray(childType); | ||
|
||
if (i > 0 && shouldTestChild) { | ||
// should test child | ||
if (i > 0 && Array.isArray(childType)) { | ||
const parentDepth = childDepth - 1; | ||
const parentType = example[i - 1]; | ||
|
||
if (typeof parentType !== 'string') | ||
throw TypeError('parent must be string'); | ||
Comment on lines
+144
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type narrowing needed due to mixed |
||
|
||
it(`(${exampleIdx})${getIndentation( | ||
childDepth, | ||
)}${parentType} (depth ${parentDepth}) > ${childType} ❌`, () => { | ||
expect( | ||
isValidChild({ | ||
parentDepth, | ||
parentType, | ||
childType, | ||
childType: childType[0], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this wasn't quite testing what it said it was testing, it was seeing if an array matches the string literal at a given level. |
||
}), | ||
).toBe(false); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,12 +105,21 @@ const parentMaxDepthLookup = { | |
[MARKDOWN_TYPE]: {}, | ||
}; | ||
|
||
export default function isValidChild({ parentType, childType, parentDepth }) { | ||
interface IsValidChildProps { | ||
parentType?: string; | ||
childType?: string; | ||
parentDepth?: unknown; | ||
} | ||
|
||
export default function isValidChild(child: IsValidChildProps): boolean { | ||
const { parentType, childType, parentDepth } = child; | ||
if (!parentType || !childType || typeof parentDepth !== 'number') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we use TypeScript, is type check here still necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends, I aimed to keep the behavior the same, taking in an unknown and returning a known type. This entire function could theoretically be completely removed, using a combo of keyof and generics can statically check most of this: interface ValidChild<
P extends keyof ParentMaxDepthLookup,
C extends keyof ParentMaxDepthLookup[P],
D extends ParentMaxDepthLookup[P][C]
> {
parentType: P;
childType: C;
parentDepth: D;
} as an intermediate step, I could set thoughts? 💭 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i feel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @graceguo-supercat I removed the generics which were causing confusion.
specifically |
||
return false; | ||
} | ||
|
||
const maxParentDepth = (parentMaxDepthLookup[parentType] || {})[childType]; | ||
const maxParentDepth: number | undefined = (parentMaxDepthLookup[ | ||
parentType | ||
] || {})[childType]; | ||
|
||
return typeof maxParentDepth === 'number' && parentDepth <= maxParentDepth; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently type narrowing using
Array.isArray
is limited to when the it is inside anif
condition