-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSDK learns __EXPERIMENTAL_strictNonNull to throw, drop objects, or r…
…eturn `| undefined` for properties, allowing for correct typesafety. (#387)
- Loading branch information
1 parent
413e511
commit b3563e0
Showing
22 changed files
with
912 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@osdk/client": minor | ||
--- | ||
|
||
OSDK learns \_\_EXPERIMENTAL_strictNonNull to throw, drop objects, or return `| undefined` for properties, allowing for correct typesafety. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { Osdk } from "@osdk/client"; | ||
import { | ||
BoundariesUsState, | ||
Employee, | ||
FooInterface, | ||
} from "@osdk/examples.basic.sdk"; | ||
import { expectType } from "ts-expect"; | ||
import { client } from "./client.js"; | ||
|
||
export async function demoStrictnessObject() { | ||
const { data: defaultResults } = await client(BoundariesUsState) | ||
.fetchPage(); | ||
expectType<string>(defaultResults[0].usState); | ||
|
||
const { data: dropResults } = await client(BoundariesUsState) | ||
.fetchPage({ $__EXPERIMENTAL_strictNonNull: "drop" }); | ||
expectType<string>(dropResults[0].usState); | ||
|
||
const { data: notStrictResults } = await client(BoundariesUsState) | ||
.fetchPage({ $__EXPERIMENTAL_strictNonNull: false }); | ||
expectType<string | undefined>(notStrictResults[0].usState); | ||
|
||
const { data: throwResults } = await client(BoundariesUsState) | ||
.fetchPage({ $__EXPERIMENTAL_strictNonNull: "throw" }); | ||
expectType<string>(throwResults[0].usState); | ||
|
||
const { data: fooDataNotStrict } = await client(FooInterface) | ||
.fetchPage({ $__EXPERIMENTAL_strictNonNull: false }); | ||
|
||
const employeeNotStrict = fooDataNotStrict[0].$as(Employee); | ||
} | ||
|
||
export async function demoStrictnessInterface() { | ||
const { data: fooDataNotStrict } = await client(FooInterface) | ||
.fetchPage({ $__EXPERIMENTAL_strictNonNull: false }); | ||
|
||
const employeeNotStrict = fooDataNotStrict[0].$as(Employee); | ||
expectType< | ||
Osdk< | ||
Employee, | ||
"$notStrict" | "firstName" | "email", | ||
"$notStrict" | "firstName" | "email" | ||
> | ||
>(employeeNotStrict); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2024 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { ObjectTypePropertyDefinition } from "@osdk/api"; | ||
import { describe, expectTypeOf, it } from "vitest"; | ||
import type { OsdkObjectPropertyType } from "./Definitions.js"; | ||
|
||
describe("OsdkObjectPropertyType", () => { | ||
describe("{ nullable: false } property", () => { | ||
const nonNullDef = { | ||
type: "string", | ||
nullable: false, | ||
} satisfies ObjectTypePropertyDefinition; | ||
|
||
it("is `| undefined` for `false`", () => { | ||
expectTypeOf<OsdkObjectPropertyType<typeof nonNullDef, false>>() | ||
.toEqualTypeOf<string | undefined>(); | ||
}); | ||
|
||
it("is not `| undefined` for `true`", () => { | ||
expectTypeOf<OsdkObjectPropertyType<typeof nonNullDef, true>>() | ||
.toEqualTypeOf<string>(); | ||
}); | ||
}); | ||
|
||
describe("{ nullable: true } property", () => { | ||
const nullableDef = { | ||
type: "string", | ||
nullable: true, | ||
} satisfies ObjectTypePropertyDefinition; | ||
|
||
it("is | undefined for `false`", () => { | ||
expectTypeOf<OsdkObjectPropertyType<typeof nullableDef, false>>() | ||
.toEqualTypeOf<string | undefined>(); | ||
}); | ||
|
||
it("is `| undefined` for `true`", () => { | ||
expectTypeOf<OsdkObjectPropertyType<typeof nullableDef, true>>() | ||
.toEqualTypeOf<string | undefined>(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.