logo
down
shadow

TS 2322 - Property 'id' is missing in type '{ id: number


TS 2322 - Property 'id' is missing in type '{ id: number

By : هدى الرحمان
Date : October 17 2020, 01:08 AM
wish of those help You are trying to assign an array of objects to a variable of type Car. Make the variable an array of Cars.
code :
cars : Car[] = [


Share : facebook icon twitter icon
TS 2322: typescript and mongoose.model as a return type to a function

TS 2322: typescript and mongoose.model as a return type to a function


By : shiju
Date : March 29 2020, 07:55 AM
it helps some times New to typescript. I was trying to create a mongoose model factory that based on a role will return the mongoose model. I will then use this model in Data Access Layer (DAL) to continue with my business layer. The issue is that I am getting the following error. TS2322:type 'typeof(../../model)is not assignable to type 'Model. Property 'findById' is missing in type 'typeof('../../model') , To import the default export you need to use this syntax :
code :
import * as mongoose from "mongoose";
import StudentNotification from './models/student'; // HERE !

function Create():mongoose.Model<mongoose.Document>{
    switch(this.userRole){
        case "Student":{
            return StudentNotification;
        }
        default :{
            return null;
        }
    }
}
import StudentNotification from './models/student';
... 
      return StudentNotification.default;
Confusing "[ts] Type ... is not assignable to type [2322]" errors inside constrained generic function

Confusing "[ts] Type ... is not assignable to type [2322]" errors inside constrained generic function


By : user2396589
Date : March 29 2020, 07:55 AM
I wish did fix the issue. After a while I realized the problem. Translating TypeScript error 2322 into plain English, it means: "You're trying to set the value of A, which has a numeric property a but which may also have additional properties (!!!), to an object literal that has only a numeric property a. Because this object literal lacks the other (potential) properties of A, the assignment fails."
As an illustration of the problem, imagine substituting A with a real type:
code :
interface A { a: number; b: string; };
const foo: A = { a: 1 };  // compiler error, as expected
function alsoWorks1<A extends {a: number}>() {
  const foo: A = {a: 1} as A;
  console.log (foo);
}
function alsoWorks2() {
  const bar = { a: 1 };
  const foo = { a: bar.a }; // no error
  console.log (foo);
}
A extends B; Type 'Pick<A, Exclude<keyof A, keyof B>> & B' is not assignable to type 'A'.ts(2322)

A extends B; Type 'Pick<A, Exclude<keyof A, keyof B>> & B' is not assignable to type 'A'.ts(2322)


By : Dilawar Khan
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The main reason is that the compiler currently doesn't perform the kind of higher-order type analysis necessary for it to understand an equivalence between an unresolved generic object type and intersections of complementary sets of properties of that type. It's not clear from the status of the linked issue whether such analysis will ever be implemented; it's no doubt possible to make the compiler account for such things, but who knows if it can be done without degrading compiler performance?
code :
interface Beta {
    x: string | number,
    y: boolean,
    z?: object  
}

interface Alpha extends Beta {
    x: string,
    y: true,
    z: undefined
}

func<Alpha, Beta>(); // no error, but uh oh

declare const beta: Beta;
declare const alpha: Alpha;
const notAlpha: Alpha = { ...alpha, ...beta }; // error!
// more strict constraint
const func = <A extends B, B extends Pick<A, keyof A & keyof B>>() => {
    const aWithoutB: Omit<A, keyof B> = {} as any;
    const b: B = {} as any;    
    const a = {
        ...aWithoutB,
        ...b
    } as A; // I'm smarter than the compiler 
};

func<Alpha, Beta>(); // error now, as desired
func<Beta & { extraProp: string }, Beta>(); // okay
Type 'string' is not assignable to type 'string & number'. ts(2322)

Type 'string' is not assignable to type 'string & number'. ts(2322)


By : Thejus R A
Date : March 29 2020, 07:55 AM
may help you . The reason your code does not work involves a little understanding of the type system.
While the expression data[name] in your code is of type string | number, any value assigned to this expression must be assignable to both string and number, or type string & number. This is clearly impossible.
code :
type Data = {
  name: string
  age: number
}

const data: Data = {
  name: 'Jack',
  age: 33,
}

function change<K extends keyof Data>(name: K, value: Data[K]): Data[K] {
  return data[name] = value
}

change('name', 'Jill') // infers change<'name'>, value: string
change('age', 21) // infers change<'age'>, value: number
Type 'void' is not assignable to type 'Lesson[]'.ts(2322) in angular 8

Type 'void' is not assignable to type 'Lesson[]'.ts(2322) in angular 8


By : Evan
Date : March 29 2020, 07:55 AM
I hope this helps you . You should not subscribe to the HTTP request within your service, as you only need to return the values on your component itself.
code :
export class LessonsService {
  constructor(private http: HttpClient) {}

  getLessons(lessonsPerPage: number, currentPage: number) {
    const queryParams = `?pagesize=${lessonsPerPage}&page=${currentPage}`;
    return this.http.get <{ message: string, lessons: any, maxLessons: number }> ('http://localhost:3000/api/lessons')
      .pipe(map(lessonData => {
        return {
          lessons: lessonData.lessons.map(lesson => {
            return {
              id: lesson.id,
              title: lesson.title,
              content: lesson.content,
              startDate: lesson.startDate,
              endDate: lesson.endDate,
              hoursStart: lesson.hoursStart,
              hoursEnd: lesson.hoursEnd,
              location: lesson.location,
              price: lesson.price,
              numberOfSessions: lesson.numberOfSessions
            };
          }),
          maxLessons: lessonData.maxLessons
        };
      }));
  }
}
ngOnInit() {
  this.lessonsService.getLessons(this.lessonsPerPage, this.currentPage).subscribe(res => {
    // do the rest here 
    this.lessons = res;
  });
}
shadow
Privacy Policy - Terms - Contact Us © soohba.com