fix(types): Make name required on transaction class #2949
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
For a long time, the
Transaction
class and theTransaction
interface have been incompatible, such that a member of theTransaction
class could not be used where an object implementing theTransaction
interface was expected. The sticking point is thename
property, which is required in the interface but optional in the class. (Sincename
isn't guaranteed to be defined on a class instance, that instance can't sub in for something which requiresname
to be defined.)We've been able to contort our way around this in the past by pointing to the less-restrictive
Span
interface, which doesn't include aname
property at all. After all, transactions are spans, and so we can capture (most) of what it means to be a transaction by just referring to what it means to be a span.But what about someplace where we actually care whether the thing we're pointing to is a full-blown transaction or merely a span? Then our workaround won't do, and we're stuck with the original problem.
Solution
One obvious answer here is to make
name
required on theTransaction
class, because then it does implement theTransaction
interface. But will that break anything? Two ways to know it won't:If we set a default value for the
name
property, then even if it's not provided, nothing goes wrong. And if that default value is the empty string (which is falsey), everyif (transaction.name)
construct will still have the same truth value as it would with an undefined name.There actually is currently no way to not provide the
name
property to theTransaction
constructor, because it takes aTransactionContext
argument as its first parameter, andname
is required there.The other solution, of course, would be to go the other way, and make
name
optional on the interface. The disadvantage there, though, is that then TS wouldn't harass people to provide a transaction name, which we actually very much want them to do. (The Discover UI is pretty bad if you don't name your transactions.) So if we can be sure that the first option is safe (which I claim it is), then it's clearly the preferable one.