paritytech/asset-transfer-api

Anti-Pattern in .toString() is only called on objects which provide useful information when stringified

Closed this issue · 0 comments

DESCRIPTION

TypeScript will call toString() on an object when it is converted to a string, such as when + adding to a string or in ${} template literals. The default Object .toString() returns "[object Object]", so this rule requires stringified objects define a more useful .toString() method. Note that Function provides its own .toString() that returns the function's code. Functions are not flagged by this rule.

BAD PRACTICE

// Passing an object or class instance to string concatenation:
'' + {};

class MyClass {}
const value = new MyClass();
value + '';

// Interpolation and manual .toString() calls too:
Value: ${value};
({}.toString());

RECOMMENDED

// These types all have useful .toString()s
'Text' + true;
Value: ${123};
Arrays too: ${[1, 2, 3]};
(() => {}).toString();

// Defining a custom .toString class is considered acceptable
class CustomToString {
toString() {
return 'Hello, world!';
}
}
Value: ${new CustomToString()};

const literalWithToString = {
toString: () => 'Hello, world!',
};

Value: ${literalWithToString};

Look here

'expectedRes' will evaluate to '[object Object]' when stringified
src/createXcmTypes/ParaToSystem.spec.ts

		};

		expect(beneficiary.toJSON()?.toString()).toStrictEqual(
			expectedRes.toString()
		);
	});
	it('Should work for V3', () => {

'expectedRes' will evaluate to '[object Object]' when stringified
src/createXcmTypes/ParaToSystem.spec.ts

		};

		expect(beneficiary.toJSON()?.toString()).toStrictEqual(
			expectedRes.toString()
		);
	});
});

'expectedRes' will evaluate to '[object Object]' when stringified
src/createXcmTypes/ParaToSystem.spec.ts

		};

		expect(destination.toJSON()?.toString()).toStrictEqual(
			expectedRes.toString()
		);
	});
	it('Should work for V3', () => {

'expectedRes' will evaluate to '[object Object]' when stringified
src/createXcmTypes/ParaToSystem.spec.ts

		};

		expect(destination.toJSON()?.toString()).toStrictEqual(
			expectedRes.toString()
		);
	});
});