Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Escriba 'cadena | null' no se puede asignar al tipo 'cadena'. El tipo 'null' no se puede asignar al tipo 'cadena'

Error: escriba 'cadena | null' no se puede asignar al tipo 'cadena'. El tipo 'null' no se puede asignar al tipo 'string'. Error: escriba '{id?: cadena | indefinido; título?: cadena | indefinido; contenido?: cadena | indefinido; }' no se puede asignar al tipo 'Publicar'. Los tipos de propiedad 'id' son incompatibles. Escriba 'cadena | undefined' no se puede asignar al tipo 'cadena'. El tipo 'indefinido' no se puede asignar al tipo 'cadena'.

 export interface Post{ id:string; title:string; content:string; }
 import { Component,OnInit} from '@angular/core'; import { NgForm } from '@angular/forms'; import { ActivatedRoute, ParamMap } from '@angular/router'; import { Post } from '../post.model'; import { PostsService } from '../posts.service'; @Component({ selector: 'app-post-create', templateUrl: './post-create.component.html', styleUrls: ['./post-create.component.css'] }) export class PostCreateComponent implements OnInit { // newPost='No Content' enteredTitle='' enteredContent='' private mode='create' private postId:string private post:Post // postId: string | null | undefined; constructor(public postsService:PostsService ,public route: ActivatedRoute){} ngOnInit(){ this.route.paramMap.subscribe((paramMap:ParamMap)=>{ if(paramMap.has('postId')){ this.mode='edit' this.postId=paramMap.get('postId') this.post=this.postsService.getPost(this.postId) }else{ this.mode='create' this.postId="null"; } }); } onAddPost(form:NgForm){ if(form.invalid) { return } console.log((form.value.title,form.value.content)); this.postsService.addPost(form.value.title,form.value.content) form.resetForm(); } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

paramMap.get() devuelve string|null . Dado que su variable es estrictamente de tipo cadena, TS muestra el error.
Puede cambiar el tipo de postId a string|null .
O haz algo como esto this.postId=paramMap.get('postId') || '';

over 4 years ago · Santiago Trujillo Report

0

Creo que paramMap devuelve un valor booleano... necesitas una cadena. "Devuelve booleano: verdadero si el mapa contiene el parámetro dado, falso de lo contrario".

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!