Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using multiple JSONSchema types in a single file#62

Answeredbycyrilletuzi
alignsoft asked this question inQ&A
Discussion options

In my Service files, I have multiple different data types that I'm fetching from localstorage via thegetItem<type>(key, { schema }) syntax.

I've defined the following:

const schema: JSONSchemaString = { type: 'string' };const schemaNum: JSONSchemaNumeric = { type: 'number' };const schemaObj: JSONSchemaObject = {    properties: {      crypt: { type: 'string' } as JSONSchemaString,    },    required: ['crypt']  };

And I get the following error when I try to use anything but 'schema' with getItem, for example:
getItem<number>(keyName, { schemaNum })

Argument of type '{ schemaNum: JSONSchemaNumeric; }' is not assignable to parameter of type 'LSGetItemOptions'.  Object literal may only specify known properties, and 'schemaNum' does not exist in type 'LSGetItemOptions'. [2345](property) schemaNum: JSONSchemaNumeric

Just usinggetItem<number>(keyName, { schema }) works fine, but I have multiple different validation signatures that I need to use.

Am I going about this the wrong way, or misunderstanding how this works?

You must be logged in to vote

In ES6+, this:

getItem<number>(keyName,{ schema})

is a shortcut for this:

getItem<number>(keyName,{schema:schema})

which works only if the property and the variable have the same name. So if your variable has another name, you can't use the shortcut, you need to do this:

getItem<number>(keyName,{schema:schemaNum})

Added the info in doc.

Replies: 1 comment

Comment options

In ES6+, this:

getItem<number>(keyName,{ schema})

is a shortcut for this:

getItem<number>(keyName,{schema:schema})

which works only if the property and the variable have the same name. So if your variable has another name, you can't use the shortcut, you need to do this:

getItem<number>(keyName,{schema:schemaNum})

Added the info in doc.

You must be logged in to vote
0 replies
Answer selected bycyrilletuzi
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@alignsoft@cyrilletuzi
Converted from issue

This discussion was converted from issue #62 on December 08, 2020 21:29.


[8]ページ先頭

©2009-2025 Movatter.jp