-3

Here we have this method:

export const showGiftedOrGiftingEmail = (
  transaction: Transaction
): { [a: string]: string } | null => {

It clearly return an optional, either an object or null. But here it accept the expression without ? operator, why?

showGiftedOrGiftingEmail(props.transaction).email

or with ?

showGiftedOrGiftingEmail(props.transaction)?.email

What is the point of use Typescript if it does not force out this kind of type safety? Swift would never accept the first expression.

János
  • 32,867
  • 38
  • 193
  • 353
  • 4
    Do you have strict null checking turned on? Strict null checking is optional, after all. – kshetline May 07 '21 at 23:04
  • "*Why web has the null and the undefined expression?*" [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/q/5076944) – VLAZ May 07 '21 at 23:14
  • 4
    How is Swift involved in this code? – VLAZ May 07 '21 at 23:14
  • For the second question I removed see e.g. https://stackoverflow.com/q/61036776/3001761 – jonrsharpe May 08 '21 at 07:05
  • @VLAZ Swift is involved because Swift implement optional pattern as well, but in a more logical way. They have only one state which represent the not existing value. Is JS null / undefined states a remnant to live with? I would only point out Swift solution is more robost and easier to understand. – János May 08 '21 at 19:42
  • @János if you don't have any Swift code, then it's not a Swift question. Otherwise we should always tag with all possible languages because *any* of them might have a solution or at least a comparison, even if we only want X. Don't abuse tags, only add ones that are relevant. – VLAZ May 08 '21 at 19:43
  • @VLAZ the problem arise in Typescript, but I am also interested about the conceptual background. I do not missue tags. Both language implement a common pattern. It is called Optional pattern, but in a different way. People who are programming only for web, maybe get used to it, but for Swift people it might seem irrational as Optional pattern is used. – János May 08 '21 at 19:49
  • Then why not tag Java, C#, C++, Python, Ruby, Haskell, etc? They all have various ways of implementing optional properties. – VLAZ May 08 '21 at 19:51
  • Why does that matter? You wanted theoretical background a minute ago. The real problem is that they don't matter. Swift doesn't matter. You had a TS question and TS question only. Just because you also know Swift, doesn't make it relevant. – VLAZ May 08 '21 at 19:52
  • I do not know them. But Swift is the cutting edge language, integrating most of fresh programming ideas. The way how they implemented may have some lesson for web programmers also. Maybe avoid use two empty state and use only one instead. This is what I am curious about. – János May 08 '21 at 19:56
  • Let me rephrase, then - can you answer your own question *using your Swift knowledge*? If the answer is "no", that's a very strong indication, that the Swift tag does not belong. Tags are only for anything *relevant to the question*. People who are knowledgeable in a tag search for questions with those tags to answer. People who have a problem with a given technology search for solutions with those tags. Other than your own knowledge of Swift, why do you expect the tag helps either answerers or future visitors in search for an answer? – VLAZ May 08 '21 at 20:03
  • People like me, who know both Swift and TypeScript and web will face with he same contradiction, wether null / undefined concept is old-fashioned? Some web people as you see can argue for it. This post here is a hub which aggregates the different opinions. If Swift tag is not included, Swift people who starts web programming might fail to notice it. – János May 08 '21 at 20:13
  • So, you don't have any good reason to include the tag, then? Swift people don't need to notice *this* question. We don't really need a bajillion dupes of a decade old questions tagged with one more language "to bring more attention". People who have a question about `null` and `undefined` should just visit the existing dupe. We don't need a special one for Python, Java, Bash, FORTRAN, COBOL, and any other language under the sun. I notice you've also tagged this with [[tag:optional]] which is also completely inappropriate as it's *not* about the same concept at all. – VLAZ May 08 '21 at 20:19

1 Answers1

1

What is the point of use Typescript if it does not force out this kind of type safety? Swift would never accept the first expression.

By default, TypeScript doesn't accept the first example either. You can see how it give an error here

If you turn off the strictNullChecks it will stop giving that error. This is the expected behavior. One of the possible uses of flag can when migrating an existing JS codebase to TS.

Why web has the null and the undefined expression? It seems overkill.

null is for values you know that you set as empty(like in your example). undefined is the default value they get when declared but not set a value. This can actually be useful for when you want to check if something is set as empty intentionally or by default.

nipuna-g
  • 6,252
  • 3
  • 31
  • 48