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
tempCreatePersonTodoRefactorSoon
function tocreatePerson
- Add an explicit inline return type to the function
- Extract that return type to an interface named
Person
- Infer the type for the
describePerson
function'sperson
parameter - Move the
Person
type to a new file,src/Person.ts
- Convert the
describePerson
function's string concatenation to a template literal string - Select the
announceExamplePeople
function's entireif
statement -including its contents- and extract it to a function in module scope namedcriticizePerson
- Move the
criticizePerson
function 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.
shell
git clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
shell
git clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
Open your editor in this project's directory:
shell
code projects/using-ide-features/temporary-code
shell
code 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:
shell
npm run tsc -- --watch
shell
npm 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-code
directory, 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.