Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define package properties as defined as soon as package.init was called #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JumpLink
Copy link
Collaborator

Description

Added improved TypeScript type definitions for package initialization to provide better type safety and developer experience. The changes include:

  1. Added UninitializedPackage and InitializedPackage interfaces to represent package state
  2. Updated type definitions to properly reflect the undefined/defined states before/after initialization
  3. Enhanced init() function type to use type assertion for post-initialization state
  4. Added example demonstrating TypeScript's type checking capabilities
  5. Updated pkg global type to reflect initialization state

@vixalien Last problem with this solution is, that this works for imports.package [props] but not for pgk[props], what do you think? Do you have any ideas how this could be done better? I could also create another import similar to @girs/gjs/doc but for @girs/gjs/pkg-defined.

Related Issue

Fixes #220

Type of Change

  • Type definition update
  • Documentation update

Validation

Test Location: examples/pkg-tsc/main.ts

Test Case:

// Before initialization - TypeScript knows these are possibly undefined
console.log('name:', imports.package.name); // type: string | undefined

// Initialize the package
imports.package.init({
    name: 'org.example.App',
    version: '1.0.0',
    prefix: '/usr/local',
    libdir: '/usr/local/lib'
});

// After initialization - TypeScript knows these are definitely defined
console.log('name:', imports.package.name); // type: string

// TypeScript type check demonstration
function requiresInitializedPackage(pkgName: string) {
    return pkgName === imports.package.name; // No type error
}

Expected Behavior:

  1. TypeScript should show proper type errors before initialization
  2. After init() call, TypeScript should recognize all package properties as defined
  3. Type checking should prevent accessing undefined properties
  4. Runtime behavior remains unchanged

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes
  • I have updated the documentation accordingly

@JumpLink JumpLink changed the title Define package properties as defined as siin as package.init was called Define package properties as defined as soon as package.init was called Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pkg.name can be undefined
1 participant