Does typescript support an implementation equivalent to trait?

Asked

Viewed 286 times

2

I am developing a project using Ionic, and would like to better organize my classes.

Below is an example of how I would do with PHP:

<?php

 trait comportamento {
         public function ficarTriste() {
         }

         public function ficarFeliz() {
         }
 }

 trait acao {
         public function falar($texto) {
                echo $texto;
         }

         public function olhar($endereco) {
                return file_get_contents($endereco);
         }
 }

 class serHumano {
      comportamento;
      acao;

      //o resto do código
 }

 $humano = new serHumano();
 $humano->ficarFeliz();
 $humano->falar('oooi');

As I could do something like this using Typescript, I used PHP as an example because it’s the language I’m most familiar with. If I’m not mistaken, that’s called partial.

Note: I did not put PHP in tags because the question is not the same, I just used it to demonstrate what I want.

1 answer

1


I think you want to use mixin which, in theory, should be even more flexible than trait. To proposal for the implementation of trait was closed because the mixin should solve this. But mixin does not provide self implementation that you want. Here the only way is to use the class or interface, implement it, and if the implementation is too complex and you want to take advantage of the code you should make a delegation to an external method.

Would something like this:

class comportamento {
    public ficarTriste() {}
    public ficarFeliz() {}
 }

 class acao {
    public falar(texto) {
        console.log(texto);
    }
    public olhar(endereco) {
        return console.log(endereco);
    }
 }

 class serHumano implements comportamento, acao {
    public ficarTriste() {}
    public ficarFeliz() {}
     public falar(texto) {
        console.log(texto);
    }
    public olhar(endereco) {
        return console.log(endereco);
    }
}

Behold "working" in the Playground. Also put on the Github for future reference.

Other than that, I can make it look like I don’t recommend.

Depending on what you need you can find another solution.

If I find any other solution I put here.

partial C# has nothing to do with it. There is a proposal for the implementation of something close to trait in the next versions of C#.

  • then @bigown but I would not like to implement these methods in each class... they will be the same for all classes. The problem is that it will have many methods and I could not extend more than one class understand?

  • I understand, but the language does not understand. It has how to do a delegation service, but with this example, nor does it compensate.

  • I get it, so let’s say Typescript doesn’t allow me to do that. Is that it? If it is just update the answer I mark as correct.

  • @Hiagosouza more or less was already, I only gave an improvement

Browser other questions tagged

You are not signed in. Login or sign up in order to post.