-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Improve type-safety when using createFileMap
#5428
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
base: main
Are you sure you want to change the base?
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 | ||||
---|---|---|---|---|---|---|
|
@@ -74,7 +74,7 @@ function SandpackRoot(props: SandpackProps) { | |||||
|
||||||
files['/styles.css'] = { | ||||||
code: [sandboxStyle, files['/styles.css']?.code ?? ''].join('\n\n'), | ||||||
hidden: !files['/styles.css']?.visible, | ||||||
hidden: files['/styles.css']?.hidden, | ||||||
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. Couldn't find neither
Suggested change
instead which would match the old behavior? |
||||||
}; | ||||||
|
||||||
return ( | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,20 +46,20 @@ const SandpackGlimmer = ({code}: {code: string}) => ( | |
); | ||
|
||
export default memo(function SandpackWrapper(props: any): any { | ||
const codeSnippet = createFileMap(Children.toArray(props.children)); | ||
const codeSnippets = Children.toArray(props.children) as React.ReactElement[]; | ||
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. Using same pattern as |
||
const files = createFileMap(codeSnippets); | ||
|
||
// To set the active file in the fallback we have to find the active file first. | ||
// If there are no active files we fallback to App.js as default. | ||
let activeCodeSnippet = Object.keys(codeSnippet).filter( | ||
let activeCodeSnippet = Object.keys(files).filter( | ||
(fileName) => | ||
codeSnippet[fileName]?.active === true && | ||
codeSnippet[fileName]?.hidden === false | ||
files[fileName]?.active === true && files[fileName]?.hidden === false | ||
); | ||
let activeCode; | ||
if (!activeCodeSnippet.length) { | ||
activeCode = codeSnippet['/App.js'].code; | ||
activeCode = files['/App.js'].code; | ||
} else { | ||
activeCode = codeSnippet[activeCodeSnippet[0]].code; | ||
activeCode = files[activeCodeSnippet[0]].code; | ||
} | ||
|
||
return ( | ||
|
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.
files
used to beany
sincecreateFileMap
returnedany
. Now that we type the input ofcreateFileMap
, TypeScript is able to infer a more accurate return type ofcreateFileMap
.