Consider this implementation of returnBothOfWhatIPassIn
:
const returnBothOfWhatIPassIn = (params: { a: unknown; b: unknown }) => {
return {
first: params.a,
second: params.b,
};
};
This time the function takes in a params
object that includes a
and b
.
import { expect, it } from 'vitest';
import { Equal, Expect } from '../helpers/type-utils';
const returnBothOfWhatIPassIn = <T1, T2>(params: { a: T1; b: T2 }) => {
return {
first: params.a,
second: params.b,
};
};
it('Should return an object where a -> first and b -> second', () => {
const result = returnBothOfWhatIPassIn({
a: 'a',
b: 1,
});
expect(result).toEqual({
first: 'a',
second: 1,
});
type test1 = Expect<
Equal<
typeof result,
{
first: string;
second: number;
}
>
>;
});
So the solution 1, we use:
const returnBothOfWhatIPassIn = <T1, T2>(params: { a: T1; b: T2 }) => {
return {
first: params.a,
second: params.b,
};
};
Solution 2:
interface Params<T1, T2> {
a: T1;
b: T2;
}
const returnBothOfWhatIPassIn = <T1, T2>(params: Params<T1, T2>) => {
return {
first: params.a,
second: params.b,
};
};
标签:Typescript,const,Object,second,returnBothOfWhatIPassIn,Approaches,params,return, From: https://www.cnblogs.com/Answer1215/p/17043906.html