how to make a function typesafe if I pass any data to it typescript code example

Example 1: typescript generic class

class Person<T, U, C> {
  name: T
  age: U
  hobby: C[]

  constructor(name: T, age: U, hobby: C[]) {
    this.name = name
    this.age = age
    this.hobby = hobby
  }
}

const output = new Person<string, number, string>('john doe', 23, ['swimming', 'traveling', 'badminton'])
console.log(output)

Example 2: typescript pass a function as an argunetn

class Foo {
    save(callback: (n: number) => any) : void {
        callback(42);
    }
}
var foo = new Foo();

var strCallback = (result: string) : void => {
    alert(result);
}
var numCallback = (result: number) : void => {
    alert(result.toString());
}

foo.save(strCallback); // not OK
foo.save(numCallback); // OK