Coder Social home page Coder Social logo

Comments (3)

danvega avatar danvega commented on June 14, 2024

I also tried using a type guard here in the filter but using the following code

    const posts : Post[] = database.results
        .filter((response) => isFullPage(response))
        .map((response ) => ({
            id: response.id,
            title: response.properties.Name["title"][0].plain_text,
            slug: response.properties.Slug["rich_text"][0].plain_text,
            cover: response.properties.Image["files"][0]?.file.url
        }));

I got the following error:

 Argument of type
PageObjectResponse | PartialPageObjectResponse | PartialDatabaseObjectResponse | DatabaseObjectResponse
is not assignable to parameter of type
PageObjectResponse | PartialPageObjectResponse
Type  PartialDatabaseObjectResponse  is not assignable to type
PageObjectResponse | PartialPageObjectResponse
Type  PartialDatabaseObjectResponse  is missing the following properties from type  PageObjectResponse :  parent, icon, cover, created_by , and  6  more.

from notion-sdk-js.

danvega avatar danvega commented on June 14, 2024

I tried to write my own type guard which seems to work a little bit better

    const posts : Post[] = database.results
        .filter(isPageObjectResponse)
        .map( (response) => ({
            id: response.id,
            title: response.properties.Name["title"][0].plain_text,
            slug: response.properties.Slug["rich_text"][0].plain_text,
            cover: response.properties.Image["files"][0]?.file.url
        }));

function isPageObjectResponse(response: any): response is PageObjectResponse {
    return !!response.properties;
}

Now it know that there is a property called properties but doesn't know about the specific properties I have like name,slug and Image.

Element implicitly has an  any  type because expression of type  "title"  can't be used to index type
{ type: "number"; number: number | null; id: string; } | { type: "url"; url: string | null; id: string; } | { type: "select"; select: SelectPropertyResponse | null; id: string; } | ... 18 more ... | { ...; }
Property  title  does not exist on type
{ type: "number"; number: number | null; id: string; } | { type: "url"; url: string | null; id: string; } | { type: "select"; select: SelectPropertyResponse | null; id: string; } | ... 18 more ... | { ...; }

from notion-sdk-js.

danvega avatar danvega commented on June 14, 2024

Finally this works but just seems hacky to me. Sorry for the rambling, just taking you through my thought process

    const posts : Post[] = database.results
        .filter(isPageObjectResponse)
        .map( (response) => ({
            id: response.id,
            title: (response.properties.Name as any)["title"][0].plain_text,
            slug: (response.properties.Slug as any)["rich_text"][0].plain_text,
            cover: (response.properties.Image as any)["files"][0]?.file.url
        }));

from notion-sdk-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.