

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Generics
incomplete
2: Multiple Type Parameters
incomplete
3: Generic Constraints
incomplete
4: Type Parameters for Types
incomplete
5: Generic Type Inference
incomplete
6: Generic Classes
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
You may have already noticed this, but in most contexts, TypeScript can infer type parameters by the actual parameters you pass in, so you won't need to specify them. Let's take our titan transformer example again:
function transform<InputType, OutputType>(
inputs: InputType[],
update: (item: InputType) => OutputType,
): OutputType[] {
const outputs: OutputType[] = [];
for (const input of inputs) {
const output = update(input);
outputs.push(output);
}
return outputs;
}
type Human = {
name: string;
age: number;
};
const humans: Human[] = [
{ name: "Eren", age: 15 },
{ name: "Mikasa", age: 16 },
{ name: "Armin", age: 15 },
];
const titanTransformer = (human: Human): string => `${human.name} is a titan!`;
Previously, we explicitly passed in <Human, string> as the type parameters:
const titanNames = transform<Human, string>(humans, titanTransformer);
console.log(titanNames);
But in this case, there's no need because TypeScript knows that our humans variable is an array of Human objects, and the titanTransformer function takes a Human and returns a string. So we can just call:
const titanNames = transform(humans, titanTransformer);
The feedback API at Support.ai returns multiple types of records – chat logs, feedback forms, moderation reports. They all include a text field, and we need a shared way to extract it.
You've been given a transform function that accepts an array and a "transformation" function. It returns a new array of transformed values.
Complete the summarizeFeedback function. It should:
text: string propertytransform function to extract the text valuestext valuesDon't pass type arguments to transform. Let TypeScript infer them.