Temporary Code
A Learning TypeScript > Using IDE Features 🍲 entree project.
Have you ever written code meant only to be temporary? Did you say something hopeful like "we'll clean this up after release" before committing it?
Well, I did. And now the "temporary" code is a core part of my record-keeping program that shouldn't change. The irony is not lost on me, thank you very much.
I'd like you to perform several refactors on the code in ./index.ts to help us modernize the old code.
Each of these should correspond to a refactor provided by TypeScript.
- Rename the
tempCreatePersonTodoRefactorSoonfunction tocreatePerson - Add an explicit inline return type to the function
- Extract that return type to an interface named
Person - Infer the type for the
describePersonfunction'spersonparameter - Move the
Persontype to a new file,src/Person.ts - Convert the
describePersonfunction's string concatenation to a template literal string - Select the
announceExamplePeoplefunction's entireifstatement -including its contents- and extract it to a function in module scope namedcriticizePerson - Move the
criticizePersonfunction to a new file,src/criticizePerson.ts
In life, there are but three certainties: death, taxes, and temporary code. -Josh Goldberg, 2022
Setup
If you haven't yet, set up the github.com/LearningTypeScript/projects repository locally.
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
Open your editor in this project's directory:
shellcode projects/using-ide-features/temporary-code
shellcode projects/using-ide-features/temporary-code
In your terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:
shellnpm run tsc -- --watch
shellnpm run tsc -- --watch
Files
src/index.ts: Refactor the functions heresrc/index.test.ts: Tests verifying the refactored functionssrc/index.solution.ts,./src/criticizePerson.solution.ts, and./src/Person.solution.ts: Solution code
Notes
Note: your terminal should be in the
temporary-codedirectory, not the root repository's directory.
- Don't directly write any code. You should be able to complete this entire project using only refactors provided by TypeScript.
- Other than any shortcuts you choose to use, the only typing you should perform on your keyboard should be selecting names in refactor prompts.