Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

typescript - Type 'unknown' is not assignable to type 'Date'

I'm new to TypeScript and not sure how to handle this. I'm using PostGraphile to generate my schema and Relay to generate graphql types. My Post table has createdAt and updatedAt fields which are 'timestamp without timezone' data type. Relay is generating this and I'm not sure why:

export type Post_post = {
  // why doesn't Relay identify these as type Date?
  ...
  readonly createdAt: unknown | null;
  readonly updatedAt: unknown | null;
};

My problem is when trying to use that 'unknown' date like this: const ca: any = new Date(createdAt); I get this:

No overload matches this call.
  Overload 1 of 4, '(value: string | number | Date): Date', gave the following error.
    Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date'.
      Type 'unknown' is not assignable to type 'Date'.
  Overload 2 of 4, '(value: ReactText): Date', gave the following error.
    Argument of type 'unknown' is not assignable to parameter of type 'ReactText'.
      Type 'unknown' is not assignable to type 'number'.  TS2769

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...