Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Type 'string' is not assignable to type 'PostCard Layout' when I am trying to assign it to @Input

I'm getting the error

Type 'string' is not assignable to type 'PostCard Layout'

I have a parent component called page-blog.component.html that I am setting the class styles in, and passing them to the child component called post-card.component.ts

In my parent HTML component when I try to set the class styling variables from the class component is when it errors out for me. Happening with the @Input() layout: PostCardLayout; section.

I found what I would feel as a dirty fix but will prob allow me to make more mistakes in the future by going into to the tsconfig.json file and setting "strictInputTypes": false,

Child class post-card.component.ts

import { Component, HostBinding, Input } from '@angular/core';

 export type PostCardLayout = 'list' | 'grid' | 'grid-sm';

 @Component({
  selector: 'app-post-card',
   templateUrl: './post-card.component.html',
  styleUrls: ['./post-card.component.scss']
 })
export class PostCardComponent {

@Input() post;

@Input() layout: PostCardLayout;

@HostBinding('class.post-card') classPostCard = true;

@HostBinding('class.post-card--layout--list') get classPostCardLayoutList(): boolean {
  return this.layout === 'list';
}

@HostBinding('class.post-card--layout--grid') get classPostCardLayoutGrid(): boolean {
  return this.layout === 'grid';
}

@HostBinding('class.post-card--layout--grid-sm') get classPostCardLayoutGridSm(): boolean {
  return this.layout === 'grid-sm';
}

constructor() { }
}

Here is the parent HTML component page-blog.component.html

                    <div class="posts-list__body">
                        <div *ngFor="let post of posts" class="posts-list__item">
                            <app-post-card
                                [post]="post"
                                [layout]="{
                                    classic: 'grid',
                                    list: 'list',
                                    grid: 'grid-sm'
                                }"
                            ></app-post-card>
                        </div>
                    </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have typed your PostCardLayout to accept one of three values:

'list' | 'grid' | 'grid-sm'

You are attempting to pass a JSON object:

[layout]="{
          classic: 'grid',
          list: 'list',
          grid: 'grid-sm'
      }[layout]"

There also appears to be a typo in that there is an extra [layout] at the end of your input.

I'm guessing between the typo and turning off the strictIputTypes your JSON is getting morphed into a string.

using "strictInputTypes": false is a terrible idea as this defeats one of the basic purposes of TypeScript.

From what I can tell from your code you want to simply assign one of the three pre-defined values:

<app-post-card
      [post]="post"
      [layout]="'grid'">
</app-post-card>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda